Part Number Hot Search : 
80C32 WTU1333 CFU1001V K1V10 103KT AVF150 C1963P 7C340
Product Description
Full Text Search
 

To Download ATTINY13-12SI Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  2535b?avr?01/04 features  high performance, low power avr ? 8-bit microcontroller  advanced risc architecture ? 120 powerful instructions ? most single clock cycle execution ? 32 x 8 general purpose working registers ? fully static operation  non-volatile program and data memories ? 1k byte of in-system programmable program memory flash endurance: 10,000 write/erase cycles ? 64 bytes in-system programmable eeprom endurance: 100,000 write/erase cycles ? 64 bytes internal sram ? programming lock for self-programming flash program and eeprom data security  peripheral features ? one 8-bit timer/counter with prescaler and two pwm channels ? 4-channel, 10-bit adc with internal voltage reference ? programmable watchdog timer with separate on-chip oscillator ? on-chip analog comparator  special microcontroller features ? debugwire on-chip debug system ? in-system programmable via spi port ? external and internal interrupt sources ? low power idle, adc noise reduction, and power-down modes ? enhanced power-on reset circuit ? programmable brown-out detection circuit ? internal calibrated oscillator  i/o and packages ? 8-pin pdip/soic: six programmable i/o lines  operating voltage: ? 1.8 - 5.5v for attiny13v ? 2.7 - 5.5v for attiny13  speed grade ? attiny13v: 0 - 6 mhz @ 1.8 - 5.5v, 0 - 12 mhz @ 2.7 - 5.5v ? attiny13: 0 - 12 mhz @ 2.7 - 5.5v, 0 - 24 mhz @ 4.5 - 5.5v  industrial temperature range  low power consumption ? active mode: 1 mhz, 1.8v: 240a ? power-down mode: < 0.1a at 1.8v pin configurations figure 1. pinout attiny13 1 2 3 4 8 7 6 5 (pcint5/reset/adc0/dw) pb5 (pcint3/clki/adc3) pb3 (pcint4/adc2) pb4 gnd vcc pb2 (sck/adc1/t0/pcint2) pb1 (miso/ain1/oc0b/int0/pcint1) pb0 (mosi/ain0/oc0a/pcint0) pdip/soic 8-bit microcontroller with 1k bytes in-system programmable flash attiny13 preliminary rev. 2535b?avr?01/04
2 attiny13 2535b?avr?01/04 overview the attiny13 is a low-power cmos 8-bit microcontroller based on the avr enhanced risc architecture. by executing powerful instructions in a single clock cycle, the attiny13 achieves throughputs approaching 1 mips per mhz allowing the system designer to optimize power consumption versus processing speed. block diagram figure 2. block diagram program counter internal oscilla tor watchdog timer stack pointer program flash sram mcu control register general purpose registers instruction register timer/ counter0 instruction decoder data dir. reg.port b data register port b programming logic timing and control mcu status register status register alu port b drivers pb0-pb5 vcc gnd control lines 8-bit databus z adc / analog comparator interrupt unit calibrated y x reset clki watchdog oscillator data eeprom
3 attiny13 2535b?avr?01/04 the avr core combines a rich instruction set with 32 general purpose working registers. all the 32 registers are directly connected to the arithmetic logic unit (alu), allowing two independent registers to be accessed in one single instruction executed in one clock cycle. the resulting architecture is more code efficient while achieving throughputs up to ten times faster than conventional cisc microcontrollers. the attiny13 provides the following features: 1k byte of in-system programmable flash, 64 bytes eeprom, 64 bytes sram, 6 general purpose i/o lines, 32 general pur- pose working registers, one 8-bit timer/counter with compare modes, internal and external interrupts, a 4-channel, 10-bi t adc, a programmable watchdog timer with internal oscillator, and three software selectable power saving modes. the idle mode stops the cpu while allowing the sram, timer/counter, adc, analog comparator, and interrupt system to continue functioning. the power-down mode saves the register con- tents, disabling all chip functions until the next interrupt or hardware reset. the adc noise reduction mode stops the cpu and all i/o modules except adc, to minimize switching noise during adc conversions. the device is manufactured using atmel?s high density non-volatile memory technology. the on-chip isp flash allows the program memory to be re-programmed in-system through an spi serial interface, by a conventional non-volatile memory programmer or by an on-chip boot code running on the avr core. the attiny13 avr is supported with a full suite of program and system development tools including: c compilers, macro assemblers, program debugger/simulators, in-cir- cuit emulators, and evaluation kits. pin descriptions vcc digital supply voltage. gnd ground. port b (pb5..pb0) port b is a 6-bit bi-directional i/o port with internal pull-up resistors (selected for each bit). the port b output buffers have symmetrical drive characteristics with both high sink and source capability. as inputs, port b pins that are externally pulled low will source current if the pull-up resistors are activated. the port b pins are tri-stated when a reset condition becomes active, even if the clock is not running. port b also serves the functions of various special features of the attiny13 as listed on page 49. reset reset input. a low level on this pin for longer than the minimum pulse length will gener- ate a reset, even if the clock is not running. the minimum pulse length is given in table 12 on page 30. shorter pulses are not guaranteed to generate a reset. about code examples this documentation contains simple code examples that briefly show how to use various parts of the device. these code examples assume that the part specific header file is included before compilation. be aware that not all c compiler vendors include bit defini- tions in the header files and interrupt handling in c is compiler dependent. please confirm with the c compiler documentation for more details.
4 attiny13 2535b?avr?01/04 avr cpu core introduction this section discusses the avr core architecture in general. the main function of the cpu core is to ensure correct program execution. the cpu must therefore be able to access memories, perform calculations, control peripherals, and handle interrupts. architectural overview figure 3. block diagram of the avr architecture in order to maximize performance and parallelism, the avr uses a harvard architecture ? with separate memories and buses for program and data. instructions in the program memory are executed with a single level pipelining. while one instruction is being exe- cuted, the next instruction is pre-fetched from the program memory. this concept enables instructions to be executed in every clock cycle. the program memory is in- system reprogrammable flash memory. the fast-access register file contains 32 x 8-bit general purpose working registers with a single clock cycle access time. this allows single-cycle arithmetic logic unit (alu) operation. in a typical alu operation, two operands are output from the register file, flash program memory instruction register instruction decoder program counter control lines 32 x 8 general purpose registrers alu status and control i/o lines eeprom data bus 8-bit data sram direct addressing indirect addressing interrupt unit watchdog timer analog comparator i/o module 2 i/o module1 i/o module n
5 attiny13 2535b?avr?01/04 the operation is executed, and the result is stored back in the register file ? in one clock cycle. six of the 32 registers can be used as three 16-bit indirect address register pointers for data space addressing ? enabling efficient address calculations. one of the these address pointers can also be used as an address pointer for look up tables in flash pro- gram memory. these added function registers are the 16-bit x-, y-, and z-register, described later in this section. the alu supports arithmetic and logic operations between registers or between a con- stant and a register. single register operations can also be executed in the alu. after an arithmetic operation, the status register is updated to reflect information about the result of the operation. program flow is provided by conditional and unconditional jump and call instructions, able to directly address the whole address space. most avr instructions have a single 16-bit word format. every program memory address contains a 16- or 32-bit instruction. during interrupts and subroutine calls, t he return address program counter (pc) is stored on the stack. the stack is effectively allocated in the general data sram, and consequently the stack size is only limited by the total sram size and the usage of the sram. all user programs must initialize the sp in the reset routine (before subroutines or interrupts are executed). the stack pointer (sp) is read/write accessible in the i/o space. the data sram can easily be accessed through the five different addressing modes supported in the avr architecture. the memory spaces in the avr architecture are all linear and regular memory maps. a flexible interrupt module has its control registers in the i/o space with an additional global interrupt enable bit in the status register. all interrupts have a separate interrupt vector in the interrupt vector table. the interrupts have priority in accordance with their interrupt vector position. the lower the interrupt vector address, the higher the priority. the i/o memory space contains 64 addresses for cpu peripheral functions as control registers, spi, and other i/o functions. the i/o memory can be accessed directly, or as the data space locations following those of the register file, 0x20 - 0x5f. alu ? arithmetic logic unit the high-performance avr alu operates in direct connection with all the 32 general purpose working registers. within a single clock cycle, arithmetic operations between general purpose registers or between a register and an immediate are executed. the alu operations are divided into three main categories ? arithmetic, logical, and bit-func- tions. some implementations of the architecture also provide a powerful multiplier supporting both signed/unsigned multiplication and fractional format. see the ?instruc- tion set? section for a detailed description.
6 attiny13 2535b?avr?01/04 status register the status register contains information about the result of the most recently executed arithmetic instruction. this information can be used for altering program flow in order to perform conditional operations. note that the status register is updated after all alu operations, as specified in the instruction set reference. this will in many cases remove the need for using the dedicated compare instructions, resulting in faster and more compact code. the status register is not automatically stored when entering an interrupt routine and restored when returning from an interrupt. this must be handled by software. the avr status register ? sreg ? is defined as:  bit 7 ? i: global interrupt enable the global interrupt enable bit must be set for the interrupts to be enabled. the individ- ual interrupt enable control is then performed in separate control registers. if the global interrupt enable register is cleared, none of the interrupts are enabled independent of the individual interrupt enable settings. the i-bit is cleared by hardware after an interrupt has occurred, and is set by the reti instruction to enable subsequent interrupts. the i- bit can also be set and cleared by the application with the sei and cli instructions, as described in the instruction set reference.  bit 6 ? t: bit copy storage the bit copy instructions bld (bit load) and bst (bit store) use the t-bit as source or destination for the operated bit. a bit from a register in the register file can be copied into t by the bst instruction, and a bit in t can be copied into a bit in a register in the register file by the bld instruction.  bit 5 ? h: half carry flag the half carry flag h indicates a half carry in some arithmetic operations. half carry is useful in bcd arithmetic. see the ?instruction set description? for detailed information.  bit 4 ? s: sign bit, s = n v the s-bit is always an exclusive or between the negative flag n and the two?s comple- ment overflow flag v. see the ?instruction set description? for detailed information.  bit 3 ? v: two?s complement overflow flag the two?s complement overflow flag v supports two?s complement arithmetics. see the ?instruction set description? for detailed information.  bit 2 ? n: negative flag the negative flag n indicates a negative result in an arithmetic or logic operation. see the ?instruction set description? for detailed information.  bit 1 ? z: zero flag the zero flag z indicates a zero result in an arithmetic or logic operation. see the ?instruction set description? for detailed information.  bit 0 ? c: carry flag the carry flag c indicates a carry in an arithmetic or logic operation. see the ?instruc- tion set description? for detailed information. bit 76543210 i t h s v n z c sreg read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
7 attiny13 2535b?avr?01/04 general purpose register file the register file is optimized for the avr enhanced risc instruction set. in order to achieve the required performance and flexibility, the following input/output schemes are supported by the register file:  one 8-bit output operand and one 8-bit result input  two 8-bit output operands and one 8-bit result input  two 8-bit output operands and one 16-bit result input  one 16-bit output operand and one 16-bit result input figure 4 shows the structure of the 32 general purpose working registers in the cpu. figure 4. avr cpu general purpose working registers most of the instructions operating on the register file have direct access to all registers, and most of them are single cycle instructions. as shown in figure 4, each register is also assigned a data memory address, mapping them directly into the first 32 locations of the user data space. although not being phys- ically implemented as sram locations, this memory organization provides great flexibility in access of the registers, as the x-, y- and z-pointer registers can be set to index any register in the file. 70addr. r0 0x00 r1 0x01 r2 0x02 ? r13 0x0d general r14 0x0e purpose r15 0x0f working r16 0x10 registers r17 0x11 ? r26 0x1a x-register low byte r27 0x1b x-register high byte r28 0x1c y-register low byte r29 0x1d y-register high byte r30 0x1e z-register low byte r31 0x1f z-register high byte
8 attiny13 2535b?avr?01/04 the x-register, y-register, and z-register the registers r26..r31 have some added functions to their general purpose usage. these registers are 16-bit address pointers for indirect addressing of the data space. the three indirect address registers x, y, and z are defined as described in figure 5. figure 5. the x-, y-, and z-registers in the different addressing modes these address registers have functions as fixed dis- placement, automatic increment, and automatic decrement (see the instruction set reference for details). stack pointer the stack is mainly used for storing temporary data, for storing local variables and for storing return addresses after interrupts and subroutine calls. the stack pointer regis- ter always points to the top of the stack. note that the stack is implemented as growing from higher memory locations to lower memory locations. this implies that a stack push command decreases the stack pointer. the stack pointer points to the data sram stack area where the subroutine and inter- rupt stacks are located. this stack space in the data sram is defined to the last address in sram during on reset. the stack pointer must be set to point above 0x60. the stack pointer is decremented by one when data is pushed onto the stack with the push instruction, and it is decremented by two when the return address is pushed onto the stack with subroutine call or interrupt. the stack pointer is incremented by one when data is popped from the stack with the pop instruction, and it is incremented by two when data is popped from the stack with return from subroutine ret or return from interrupt reti. the avr stack pointer is implemented as two 8-bit registers in the i/o space. the num- ber of bits actually used is implementation dependent. note that the data space in some implementations of the avr architecture is so small that only spl is needed. in this case, the sph register will not be present. 15 xh xl 0 x-register 7 0 7 0 r27 (0x1b) r26 (0x1a) 15 yh yl 0 y-register 7 0 7 0 r29 (0x1d) r28 (0x1c) 15 zh zl 0 z-register 7 0 7 0 r31 (0x1f) r30 (0x1e) bit 151413121110 9 8 sp15 sp14 sp13 sp12 sp11 sp10 sp9 sp8 sph sp7 sp6 sp5 sp4 sp3 sp2 sp1 sp0 spl 76543210 read/write r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 10011111
9 attiny13 2535b?avr?01/04 instruction execution timing this section describes the general access timing concepts for instruction execution. the avr cpu is driven by the cpu clock clk cpu , directly generated from the selected clock source for the chip. no internal clock division is used. figure 6 shows the parallel instruction fetches and instruction executions enabled by the harvard architecture and the fast access register file concept. this is the basic pipelin- ing concept to obtain up to 1 mips per mhz with the corresponding unique results for functions per cost, functions per clocks, and functions per power-unit. figure 6. the parallel instruction fetches and instruction executions figure 7 shows the internal timing concept for the register file. in a single clock cycle an alu operation using two register operands is executed, and the result is stored back to the destination register. figure 7. single cycle alu operation reset and interrupt handling the avr provides several different interrupt sources. these interrupts and the separate reset vector each have a separate program vector in the program memory space. all interrupts are assigned individual enable bits which must be written logic one together with the global interrupt enable bit in the status register in order to enable the interrupt. the lowest addresses in the program memory space are by default defined as the reset and interrupt vectors. the complete list of vectors is shown in ?interrupts? on page 40. the list also determines the priority levels of the different interrupts. the lower the address the higher is the priority level. reset has the highest priority, and next is int0 ? the external interrupt request 0. when an interrupt occurs, the global interrupt enable i-bit is cleared and all interrupts are disabled. the user software can write logic one to the i-bit to enable nested inter- rupts. all enabled interrupts can then interrupt the current interrupt routine. the i-bit is automatically set when a return from interrupt instruction ? reti ? is executed. clk 1st instruction fetch 1st instruction execute 2nd instruction fetch 2nd instruction execute 3rd instruction fetch 3rd instruction execute 4th instruction fetch t1 t2 t3 t4 cpu total execution time register operands fetch alu operation execute result write back t1 t2 t3 t4 clk cpu
10 attiny13 2535b?avr?01/04 there are basically two types of interrupts. the first type is triggered by an event that sets the interrupt flag. for these interrupts, the program counter is vectored to the actual interrupt vector in order to exec ute the interrupt handling routine, and hardware clears the corresponding interrupt flag. interrupt flags can also be cleared by writing a logic one to the flag bit position(s) to be cleared. if an interrupt condition occurs while the corresponding interrupt enable bit is cleared, the interrupt flag will be set and remem- bered until the interrupt is enabled, or the flag is cleared by software. similarly, if one or more interrupt conditions occur while the global interrupt enable bit is cleared, the cor- responding interrupt flag(s) will be set and remembered until the global interrupt enable bit is set, and will then be executed by order of priority. the second type of interrupts will trigger as long as the interrupt condition is present. these interrupts do not necessarily have interrupt flags. if the interrupt condition disap- pears before the interrupt is enabled, the interrupt will not be triggered. when the avr exits from an interrupt, it will always return to the main program and exe- cute one more instruction before any pending interrupt is served. note that the status register is not automatically stored when entering an interrupt rou- tine, nor restored when returning from an interrupt routine. this must be handled by software. when using the cli instruction to disable interrupts, the interrupts will be immediately disabled. no interrupt will be executed after the cli instruction, even if it occurs simulta- neously with the cli instruction. the following example shows how this can be used to avoid interrupts during the timed eeprom write sequence.. assembly code example in r16, sreg ; store sreg value cli ; disable interrupts during timed sequence sbi eecr, eemwe ; start eeprom write sbi eecr, eewe out sreg, r16 ; restore sreg value (i-bit) c code example char csreg; csreg = sreg; /* store sreg value */ /* disable interrupts during timed sequence */ _cli(); eecr |= (1< 11 attiny13 2535b?avr?01/04 when using the sei instruction to enable interrupts, the instruction following sei will be executed before any pending interrupts, as shown in this example. interrupt response time the interrupt execution response for all the enabled avr interrupts is four clock cycles minimum. after four clock cycles the program vector address for the actual interrupt handling routine is executed. during this four clock cycle period, the program counter is pushed onto the stack. the vector is normally a jump to the interrupt routine, and this jump takes three clock cycles. if an interrupt occurs during execution of a multi-cycle instruction, this instruction is completed before the interrupt is served. if an interrupt occurs when the mcu is in sleep mode, the interrupt execution response time is increased by four clock cycles. this increase comes in addition to the start-up time from the selected sleep mode. a return from an interrupt handling routine takes four clock cycles. during these four clock cycles, the program counter (two bytes) is popped back from the stack, the stack pointer is incremented by two, and the i-bit in sreg is set. assembly code example sei ; set global interrupt enable sleep ; enter sleep, waiting for interrupt ; note: will enter sleep before any pending ; interrupt(s) c code example _sei(); /* set global interrupt enable */ _sleep(); /* enter sleep, waiting for interrupt */ /* note: will enter sleep before any pending interrupt(s) */
12 attiny13 2535b?avr?01/04 avr attiny13 memories this section describes the different memories in the attiny13. the avr architecture has two main memory spaces, the data memory and the program memory space. in addition, the attiny13 features an eeprom memory for data storage. all three memory spaces are linear and regular. in-system re- programmable flash program memory the attiny13 contains 1k byte on-chip in-system reprogrammable flash memory for program storage. since all avr instructions are 16 or 32 bits wide, the flash is orga- nized as 512 x 16. the flash memory has an endurance of at least 10,000 write/erase cycles. the attiny13 program counter (pc) is nine bits wide, thus addressing the 512 program memory locations. ?memory programming? on page 100 contains a detailed description on flash data serial downloading using the spi pins. constant tables can be allocated within the entire program memory address space (see the lpm ? load program memory instruction description). timing diagrams for instruction fetch and execution are presented in ?instruction execu- tion timing? on page 9. figure 8. program memory map 0x0000 0x01ff program memory
13 attiny13 2535b?avr?01/04 sram data memory figure 9 shows how the attiny13 sram memory is organized. the lower 160 data memory locations address both the register file, the i/o memory and the internal data sram. the first 32 locations address the register file, the next 64 locations the standard i/o memory, and the last 64 locations address the internal data sram. the five different addressing modes for the data memory cover: direct, indirect with displacement, indirect, indirect with pre-decrement, and indirect with post-increment. in the register file, registers r26 to r31 feature the indirect addressing pointer registers. the direct addressing reaches the entire data space. the indirect with displacement mode reaches 63 address locations from the base address given by the y- or z-register. when using register indirect addressing modes with automatic pre-decrement and post- increment, the address registers x, y, and z are decremented or incremented. the 32 general purpose working registers, 64 i/o registers, and the 64 bytes of internal data sram in the attiny13 are all accessible through all these addressing modes. the register file is described in ?general purpose register file? on page 7. figure 9. data memory map data memory access times this section describes the general access timing concepts for internal memory access. the internal data sram access is performed in two clk cpu cycles as described in figure 10. figure 10. on-chip data sram access cycles 32 registers 64 i/o registers internal sram (64 x 8) 0x0000 - 0x001f 0x0020 - 0x005f 0x009f 0x0060 data memory clk wr rd data data address address valid t1 t2 t3 compute address read write cpu memory access instruction next instruction
14 attiny13 2535b?avr?01/04 eeprom data memory the attiny13 contains 64 bytes of data eeprom memory. it is organized as a separate data space, in which single bytes can be read and written. the eeprom has an endur- ance of at least 100,000 write/erase cycles. the access between the eeprom and the cpu is described in the following, specifying the eeprom address registers, the eeprom data register, and the eeprom control register. for a detailed description of serial data downloading to the eeprom, see page 103. eeprom read/write access the eeprom access registers are accessible in the i/o space. the write access times for the eeprom are given in table 1. a self-timing function, however, lets the user software detect when the next byte can be written. if the user code contains instructions that write the eeprom, some precautions must be taken. in heavily filtered power supplies, v cc is likely to rise or fall slowly on power-up/down. this causes the device for some period of time to run at a voltage lower than specified as minimum for the clock frequency used. see ?preventing eeprom corruption? on page 18 for details on how to avoid problems in these situations. in order to prevent unintentional eeprom writes, a specific write procedure must be fol- lowed. refer to ?atomic byte programming? on page 16 and ?split byte programming? on page 16 for details on this. when the eeprom is read, the cpu is hal ted for four clock cycles before the next instruction is executed. when the eeprom is written, the cpu is halted for two clock cycles before the next instruction is executed. eeprom address register ? eearl  bits 7..6 ? res: reserved bits these bits are reserved bits in the attiny13 and will always read as zero.  bits 5..0 ? eear5..0: eeprom address the eeprom address register ? eearl ? specifies the eeprom address in the 64 bytes eeprom space. the eeprom data bytes are addressed linearly between 0 and 63. the initial value of eearl is undefined. a proper value must be written before the eeprom may be accessed. eeprom data register ? eedr  bits 7..0 ? eedr7..0: eeprom data for the eeprom write operation the eedr register contains the data to be written to the eeprom in the address given by the eearl register. for the eeprom read oper- ation, the eedr contains the data read out from the eeprom at the address given by eearl. bit 76543210 ? ? eear5 eear4 eear3 eear2 eear1 eear0 eearl read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 x x x x x x bit 76543210 eedr7 eedr6 eedr5 eedr4 eedr3 eedr2 eedr1 eedr0 eedr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value x x x x x x x x
15 attiny13 2535b?avr?01/04 eeprom control register ? eecr  bit 7 ? res: reserved bit this bit is reserved for future use and will always read as 0 in attiny13. for compatibility with future avr devices, always write this bit to zero. after reading, mask out this bit.  bit 6 ? res: reserved bit this bit is reserved in the attiny13 and will always read as zero.  bits 5, 4 ? eepm1 and eepm0: eeprom programming mode bits the eeprom programming mode bits setting defines which programming action that will be triggered when writing eepe. it is possible to program data in one atomic opera- tion (erase the old value and program the new value) or to split the erase and write operations in two different operations. the programming times for the different modes are shown in table 1. while eepe is set, any write to eepmn will be ignored. during reset, the eepmn bits will be reset to 0b00 unless the eeprom is busy programming.  bit 3 ? eerie: eeprom ready interrupt enable writing eerie to one enables the eeprom ready interrupt if the i-bit in sreg is set. writing eerie to zero disables the interrupt. the eeprom ready interrupt generates a constant interrupt when non-volatile memory is ready for programming.  bit 2 ? eempe: eeprom master program enable the eempe bit determines whether writing eepe to one will have effect or not. when eempe is set, setting eepe within four clock cycles will program the eeprom at the selected address. if eempe is zero, setting eepe will have no effect. when eempe has been written to one by software, hardware clears the bit to zero after four clock cycles.  bit 1 ? eepe: eeprom program enable the eeprom program enable signal eepe is the programming enable signal to the eeprom. when eepe is written, the eeprom will be programmed according to the eepmn bits setting. the eempe bit must be written to one before a logical one is writ- ten to eepe, otherwise no eeprom write takes place. when the write access time has elapsed, the eepe bit is cleared by hardware. when eepe has been set, the cpu is halted for two cycles before the next instruction is executed.  bit 0 ? eere: eeprom read enable the eeprom read enable signal ? eere ? is the read strobe to the eeprom. when the correct address is set up in the eearl register, the eere bit must be written to bit 76543210 ? ? eepm1 eepm0 eerie eempe eepe eere eecr read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 x x 0 0 x 0 table 1. eeprom mode bits eepm1 eepm0 programming time operation 0 0 3.4 ms erase and write in one operation (atomic operation) 0 1 1.8 ms erase only 1 0 1.8 ms write only 1 1 ? reserved for future use
16 attiny13 2535b?avr?01/04 one to trigger the eeprom read. the eeprom read access takes one instruction, and the requested data is available immediately. when the eeprom is read, the cpu is halted for four cycles before the next instruction is executed. the user should poll the eepe bit before starting the read operation. if a write operation is in progress, it is nei- ther possible to read the eeprom, nor to change the eearl register. atomic byte programming using atomic byte programming is the simplest mode. when writing a byte to the eeprom, the user must write the address into the eearl register and data into eedr register. if the eepmn bits are zero, writing eepe (within four cycles after eempe is written) will trigger the erase/write operat ion. both the erase and write cycle are done in one operation and the total programming time is given in table 1. the eepe bit remains set until the erase and write operations are completed. while the device is busy with programming, it is not possible to do any other eeprom operations. split byte programming it is possible to split the erase and write cycl e in two different operations. this may be useful if the system requires short access time for some limited period of time (typically if the power supply voltage falls). in order to take advantage of this method, it is required that the locations to be written have been erased before the write operation. but since the erase and write operations are split, it is possible to do the erase operations when the system allows doing time-critical operations (typically after power-up). erase to erase a byte, the address must be written to eearl. if the eepmn bits are 0b01, writing the eepe (within four cycles after eempe is written) will tri gger the erase opera- tion only (programming time is given in table 1). the eepe bit remains set until the erase operation completes. while the device is busy programming, it is not possible to do any other eeprom operations. write to write a location, the user must write the address into eearl and the data into eedr. if the eepmn bits are 0b10, writing the eepe (within four cycles after eempe is written) will trigger the write operation only (programming time is given in table 1). the eepe bit remains set until the write operation completes. if the location to be written has not been erased before write, the data that is stored must be considered as lost. while the device is busy with programming, it is not possible to do any other eeprom operations. the calibrated oscillator is used to time the eeprom accesses. make sure the oscilla- tor frequency is within the requirements described in ?oscillator calibration register ? osccal? on page 22.
17 attiny13 2535b?avr?01/04 the following code examples show one assembly and one c function for erase, write, or atomic write of the eeprom. the examples assume that interrupts are controlled (e.g., by disabling interrupts globally) so that no interrupts will occur during execution of these functions. assembly code example eeprom_write: ; wait for completion of previous write sbic eecr,eepe rjmp eeprom_write ; set programming mode ldi r16, (0<>eepm0) /* set up address and data registers */ eearl = ucaddress; eedr = ucdata; /* write logical one to eemwe */ eecr |= (1< 18 attiny13 2535b?avr?01/04 the next code examples show assembly and c functions for reading the eeprom. the examples assume that interrupts are controlled so that no interrupts will occur during execution of these functions. preventing eeprom corruption during periods of low v cc , the eeprom data can be corrupted because the supply volt- age is too low for the cpu and the eeprom to operate properly. these issues are the same as for board level systems using eeprom, and the same design solutions should be applied. an eeprom data corruption can be caused by two situations when the voltage is too low. first, a regular write sequence to the eeprom requires a minimum voltage to operate correctly. secondly, the cpu itself can execute instructions incorrectly, if the supply voltage is too low. eeprom data corruption can easily be avoided by following this design recommendation: keep the avr reset active (low) during periods of insufficient power supply voltage. this can be done by enabling the internal brown-out detector (bod). if the detection level of the internal bod does not match the needed detection level, an external low v cc reset protection circuit can be used. if a reset occurs while a write operation is in progress, the write operation will be completed provided that the power supply voltage is sufficient. assembly code example eeprom_read: ; wait for completion of previous write sbic eecr,eepe rjmp eeprom_read ; set up address (r17) in address register out eearl, r17 ; start eeprom read by writing eere sbi eecr,eere ; read data from data register in r16,eedr ret c code example unsigned char eeprom_read( unsigned char ucaddress) { /* wait for completion of previous write */ while(eecr & (1< 19 attiny13 2535b?avr?01/04 i/o memory the i/o space definition of the attiny13 is shown in ?register summary? on page 153. all attiny13 i/os and peripherals are placed in the i/o space. all i/o locations may be accessed by the ld/lds/ldd and st/sts/std instructions, transferring data between the 32 general purpose working registers and the i/o space. i/o registers within the address range 0x00 - 0x1f are directly bit-accessible using the sbi and cbi instruc- tions. in these registers, the value of single bits can be checked by using the sbis and sbic instructions. refer to the instruction set section for more details. when using the i/o specific commands in and out, the i/o addresses 0x00 - 0x3f must be used. when addressing i/o registers as data space using ld and st instructions, 0x20 must be added to these addresses. for compatibility with future devices, reserved bits should be written to zero if accessed. reserved i/o memory addresses should never be written. some of the status flags are cleared by writing a logical one to them. note that, unlike most other avrs, the cbi and sbi instructions will only operate on the specified bit, and can therefore be used on registers containing such status flags. the cbi and sbi instructions work with registers 0x00 to 0x1f only. the i/o and peripherals control registers are explained in later sections.
20 attiny13 2535b?avr?01/04 system clock and clock options clock systems and their distribution figure 11 presents the principal clock systems in the avr and their distribution. all of the clocks need not be active at a given time. in order to reduce power consumption, the clocks to modules not being used can be halted by using different sleep modes, as described in ?power management and sleep modes? on page 26. the clock systems are detailed below. figure 11. clock distribution cpu clock ? clk cpu the cpu clock is routed to parts of the system concerned with operation of the avr core. examples of such modules are the general purpose register file, the status reg- ister and the data memory holding the stack pointer. halting the cpu clock inhibits the core from performing general operations and calculations. i/o clock ? clk i/o the i/o clock is used by the majority of the i/o modules, like timer/counter. the i/o clock is also used by the external interrupt module, but note that some external inter- rupts are detected by asynchronous logic, allowing such interrupts to be detected even if the i/o clock is halted. flash clock ? clk flash the flash clock controls operation of the flash interface. the flash clock is usually active simultaneously with the cpu clock. adc clock ? clk adc the adc is provided with a dedicated clock domain. this allows halting the cpu and i/o clocks in order to reduce noise generated by digital circuitry. this gives more accu- rate adc conversion results. general i/o modules cpu core ram clk i/o avr clock control unit clk cpu flash and eeprom clk flash source clock watchdog timer watchdog oscillator reset logic clock multiplexer watchdog clock calibrated rc oscillator external clock adc clk adc
21 attiny13 2535b?avr?01/04 clock sources the device has the following clock source options, selectable by flash fuse bits as shown below. the clock from the selected source is input to the avr clock generator, and routed to the appropriate modules. note: 1. for all fuses ?1? means unprogrammed while ?0? means programmed. the various choices for each clocking option is given in the following sections. when the cpu wakes up from power-down or power-save, the selected clock source is used to time the start-up, ensuring stable oscillator operation before instruction execution starts. when the cpu starts from reset, there is an additional delay allowing the power to reach a stable level before commencing normal operation. the watchdog oscillator is used for timing this real-time part of the star t-up time. the number of wdt oscillator cycles used for each time-out is shown in table 3. default clock source the device is shipped with cksel = ?10?, sut = ?10?, and ckdiv8 programmed. the default clock source setting is therefore the internal rc oscillator running at 9.6 mhz with longest start-up time and an initial system clock prescaling of 8. this default setting ensures that all users can make their desired clock source setting using an in-system or high-voltage programmer. table 2. device clocking options select (1) device clocking option cksel1..0 calibrated internal rc oscillator 01, 10 external clock 00 128 khz internal oscillator 11 table 3. number of watchdog oscillator cycles typ time-out number of cycles 4 ms 512 64 ms 8k (8,192)
22 attiny13 2535b?avr?01/04 calibrated internal rc oscillator the calibrated internal rc oscillator provides an 9.6 mhz or 4.8 mhz clock. the fre- quency is the nominal value at 3v and 25 c. if the frequency exceeds the specification of the device (depends on v cc ), the ckdiv8 fuse must be programmed in order to divide the internal frequency by 8 during start-up. see ?system clock prescaler? on page 24. for more details. this clock may be selected as the system clock by program- ming the cksel fuses as shown in table 4. if selected, it will operate with no external components. during reset, hardware loads the calibration byte into the osccal regis- ter and thereby automatically calibrates the rc oscillator. at 3v and 25 c, this calibration gives a frequency within 10% of the nominal frequency. using run-time cal- ibration methods as described in application notes available at www.atmel.com/avr it is possible to achieve 3% accuracy at any given v cc and temperature. when this oscil- lator is used as the chip clock, the watchdog oscillator will still be used for the watchdog timer and for the reset time-out. for more information on the pre-pro- grammed calibration value, see the section ?calibration byte? on page 102. note: 1. the device is shipped with this option selected. when this oscillator is selected, start-up times are determined by the sut fuses as shown in table 5.. note: 1. the device is shipped with this option selected. oscillator calibration register ? osccal  bit 7 ? res: reserved bit this bit is reserved bit in the attiny13 and will always read as zero.  bits 6..0 ? cal6..0: oscillator calibration value writing the calibration byte to this address will trim the internal oscillator to remove pro- cess variations from the oscillator frequency. this is done automatically during chip reset. when osccal is zero, the lowest available frequency is chosen. writing non- zero values to this register will increase the frequency of the internal oscillator. writing 0x7f to the register gives the highest available frequency. the calibrated oscillator is used to time eeprom and flash access. if eeprom or flash is written, do not cali- brate to more than 2% above the nominal frequency. otherwise, the eeprom or flash table 4. internal calibrated rc oscillator operating modes cksel1..0 nominal frequency 10 (1) 9.6 mhz 01 4.8 mhz table 5. start-up times for the internal calibrated rc oscillator clock selection sut1..0 start-up time from power-down additional delay from reset (v cc = 5.0v) recommended usage 00 6 ck 14ck bod enabled 01 6 ck 14ck + 4 ms fast rising power 10 (1) 6 ck 14ck + 64 ms slowly rising power 11 reserved bit 76543210 ? cal6 cal5 cal4 cal3 cal2 cal1 cal0 osccal read/write r r/w r/w r/w r/w r/w r/w r/w initial value 0 device specific calibration value
23 attiny13 2535b?avr?01/04 write may fail. note that the oscillator is intended for calibration to 9.6 mhz or 4.8 mhz. tuning to other values is not guaranteed, as indicated in table 6. avoid changing the calibration value in large steps when calibrating the calibrated inter- nal rc oscillator to ensure stable operation of the mcu. a variation in frequency of more than 2% from one cycle to the next can lead to unpredictable behavior. changes in osccal should not exceed 0x20 for each calibration. external clock to drive the device from an external clock source, clki should be driven as shown in figure 12. to run the device on an external clock, the cksel fuses must be pro- grammed to ?00?. figure 12. external clock drive configuration when this clock source is selected, start-up times are determined by the sut fuses as shown in table 7. when applying an external clock, it is required to avoid sudden changes in the applied clock frequency to ensure stable operation of the mcu. a variation in frequency of more than 2% from one clock cycle to the next can lead to unpredictable behavior. it is required to ensure that the mcu is kept in reset during such changes in the clock frequency. note that the system clock prescaler can be used to implement run-time changes of the internal clock frequency while still ensuring stable operation. refer to ?system clock prescaler? on page 24 for details. table 6. internal rc oscillator frequency range osccal value min frequency in percentage of nominal frequency max frequency in percentage of nominal frequency 0x00 50% 100% 0x3f 75% 150% 0x7f 100% 200% table 7. start-up times for the external clock selection sut1..0 start-up time from power- down and power-save additional delay from reset recommended usage 00 6 ck 14ck bod enabled 01 6 ck 14ck + 4 ms fast rising power 10 6 ck 14ck + 64 ms slowly rising power 11 reserved external clock signal clki gnd
24 attiny13 2535b?avr?01/04 128 khz internal oscillator the 128 khz internal oscillator is a low power oscillator providing a clock of 128 khz. the frequency is nominal at 3v and 25 c. this clock may be select as the system clock by programming the cksel fuses to ?11?. when this clock source is selected, start-up times are determined by the sut fuses as shown in table 8. system clock prescaler the attiny13 system clock can be divided by setting the clock prescale register ? clkpr. this feature can be used to decrease power consumption when the require- ment for processing power is low. this can be used with all clock source options, and it will affect the clock frequency of the cpu and all synchronous peripherals. clk i/o , clk adc , clk cpu , and clk flash are divided by a factor as shown in table 9. clock prescale register ? clkpr  bit 7 ? clkpce: clock prescaler change enable the clkpce bit must be written to logic one to enable change of the clkps bits. the clkpce bit is only updated when the other bits in clkpr are simultaneously written to zero. clkpce is cleared by hardware four cycles after it is written or when the clkps bits are written. rewriting the clkpce bit within this time-out period does neither extend the time-out period, nor clear the clkpce bit.  bits 6..4 ? res: reserved bits these bits are reserved bits in the attiny13 and will always read as zero.  bits 3..0 ? clkps3..0: clock prescaler select bits 3 - 0 these bits define the division factor between the selected clock source and the internal system clock. these bits can be written run-time to vary the clock frequency to suit the application requirements. as the divider divides the master clock input to the mcu, the speed of all synchronous peripherals is reduced when a division factor is used. the divi- sion factors are given in table 9. to avoid unintentional changes of clock frequency, a special write procedure must be followed to change the clkps bits: 1. write the clock prescaler change enable (clkpce) bit to one and all other bits in clkpr to zero. 2. within four cycles, write the desired value to clkps while writing a zero to clkpce. interrupts must be disabled when changing prescaler setting to make sure the write pro- cedure is not interrupted. table 8. start-up times for the 128 khz internal oscillator sut1..0 start-up time from power- down and power-save additional delay from reset recommended usage 00 6 ck 14ck bod enabled 01 6 ck 14ck + 4 ms fast rising power 10 6 ck 14ck + 64 ms slowly rising power 11 reserved bit 76543210 clkpce ? ? ? clkps3 clkps2 clkps1 clkps0 clkpr read/write r/w r r r r/w r/w r/w r/w initial value 0 0 0 0 see bit description
25 attiny13 2535b?avr?01/04 the ckdiv8 fuse determines the initial value of the clkps bits. if ckdiv8 is unpro- grammed, the clkps bits will be reset to ?0000?. if ckdiv8 is programmed, clkps bits are reset to ?0011?, giving a division factor of eight at start up. this feature should be used if the selected clock source has a higher frequency than the maximum frequency of the device at the present operating conditions. note that any value can be written to the clkps bits regardless of the ckdiv8 fuse setting. the application software must ensure that a sufficient division factor is chosen if the selected clock source has a higher frequency than the maximum frequency of the device at the present operating condi- tions. the device is shipped with the ckdiv8 fuse programmed. switching time when switching between prescaler settings, the system clock prescaler ensures that no glitches occur in the clock system and that no intermediate frequency is higher than neither the clock frequency corresponding to the previous setting, nor the clock fre- quency corresponding to the new setting. the ripple counter that implements the prescaler runs at the frequency of the undivided clock, which may be faster than the cpu?s clock frequency. hence, it is not possible to determine the state of the prescaler ? even if it were readable, and the exact time it takes to switch from one clock division to another cannot be exactly predicted. from the time the clkps values are written, it takes between t1 + t2 and t1 + 2*t2 before the new clock frequency is active. in this interval, 2 active clock edges are pro- duced. here, t1 is the previous clock period, and t2 is the period corresponding to the new prescaler setting. table 9. clock prescaler select clkps3 clkps2 clkps1 clkps0 clock division factor 0000 1 0001 2 0010 4 0011 8 0100 16 0101 32 0110 64 0111 128 1000 256 1 0 0 1 reserved 1 0 1 0 reserved 1 0 1 1 reserved 1 1 0 0 reserved 1 1 0 1 reserved 1 1 1 0 reserved 1 1 1 1 reserved
26 attiny13 2535b?avr?01/04 power management and sleep modes the high performance and industry leading code efficiency makes the avr microcon- trollers an ideal choice for low power applications. sleep modes enable the application to shut down unused modules in the mcu, thereby saving power. the avr provides various sleep modes allowing the user to tailor the power consumption to the application?s requirements. to enter any of the three sleep modes, the se bit in mcucr must be written to logic one and a sleep instruction must be executed. the sm1..0 bits in the mcucr regis- ter select which sleep mode (idle, adc noise reduction, or power-down) will be activated by the sleep instruction. see table 10 for a summary. if an enabled interrupt occurs while the mcu is in a sleep mode, the mcu wakes up. the mcu is then halted for four cycles in addition to the start-up time, executes the interrupt routine, and resumes execution from the instruction following sleep. the contents of the register file and sram are unaltered when the device wakes up from sleep. if a reset occurs during sleep mode, the mcu wakes up and executes from the reset vector. figure 11 on page 20 presents the different clock systems in the attiny13, and their dis- tribution. the figure is helpful in selecting an appropriate sleep mode. mcu control register ? mcucr the mcu control register contains control bits for power management.  bit 5 ? se: sleep enable the se bit must be written to logic one to make the mcu enter the sleep mode when the sleep instruction is executed. to avoid the mcu entering the sleep mode unless it is the programmer?s purpose, it is recommended to write the sleep enable (se) bit to one just before the execution of the sleep instruction and to clear it immediately after wak- ing up.  bits 4, 3 ? sm1..0: sleep mode select bits 2..0 these bits select between the three available sleep modes as shown in table 10.  bit 2 ? res: reserved bit this bit is a reserved bit in the attiny13 and will always read as zero. bit 76543210 ? pud se sm1 sm0 ? isc01 isc00 mcucr read/write r r/w r/w r/w r/w r r/w r/w initial value00000000 table 10. sleep mode select sm1 sm0 sleep mode 00idle 0 1 adc noise reduction 10power-down 11reserved
27 attiny13 2535b?avr?01/04 idle mode when the sm1..0 bits are written to 00, the sleep instruction makes the mcu enter idle mode, stopping the cpu but allowing analog comparator, adc, timer/counter, watchdog, and the interrupt system to continue operating. this sleep mode basically halts clk cpu and clk flash , while allowing the other clocks to run. idle mode enables the mcu to wake up from external triggered interrupts as well as internal ones like the timer overflow. if wake-up from the analog comparator interrupt is not required, the analog comparator can be powered down by setting the acd bit in the analog comparator control and status register ? acsr. this will reduce power consumption in idle mode. if the adc is enabled, a conversion starts automatically when this mode is entered. adc noise reduction mode when the sm1..0 bits are written to 01, the sleep instruction makes the mcu enter adc noise reduction mode, stopping the cpu but allowing the adc, the external inter- rupts, and the watchdog to continue operating (if enabled). this sleep mode halts clk i/o , clk cpu , and clk flash , while allowing the other clocks to run. this improves the noise environment for the adc, enabling higher resolution measure- ments. if the adc is enabled, a conversion starts automatically when this mode is entered. apart form the adc conversion complete interrupt, only an external reset, a watchdog reset, a brown-out reset, an spm/eeprom ready interrupt, an external level interrupt on int0 or a pin change interrupt can wake up the mcu from adc noise reduction mode. power-down mode when the sm1..0 bits are written to 10, the sleep instruction makes the mcu enter power-down mode. in this mode, the oscillator is stopped, while the external interrupts, and the watchdog continue operating (if enabled). only an external reset, a watchdog reset, a brown-out reset, an external level interrupt on int0, or a pin change interrupt can wake up the mcu. this sleep mode halts all generated clocks, allowing operation of asynchronous modules only. note that if a level triggered interrupt is used for wake-up from power-down mode, the changed level must be held for some time to wake up the mcu. refer to ?external inter- rupts? on page 52 for details. note: 1. for int0, only level interrupt. table 11. active clock domains and wake-up sources in the different sleep modes active clock domains oscillators wake-up sources sleep mode clk cpu clk flash clk io clk adc main clock source enabled int0 and pin change spm/ eeprom ready adc other i/o watchdog interrupt idle x x x x x x x x adc noise reduction x x x (1) xx x power-down x (1) x
28 attiny13 2535b?avr?01/04 minimizing power consumption there are several issues to consider when trying to minimize the power consumption in an avr controlled system. in general, sleep modes should be used as much as possi- ble, and the sleep mode should be selected so that as few as possible of the device?s functions are operating. all functions not needed should be disabled. in particular, the following modules may need special consideration when trying to achieve the lowest possible power consumption. analog to digital converter if enabled, the adc will be enabled in all sleep modes. to save power, the adc should be disabled before entering any sleep mode. when the adc is turned off and on again, the next conversion will be an extended conversion. refer to ?analog to digital con- verter? on page 77 for details on adc operation. analog comparator when entering idle mode, the analog comparator should be disabled if not used. when entering adc noise reduction mode, the analog comparator should be disabled. in the other sleep modes, the analog comparator is automatically disabled. however, if the analog comparator is set up to use the internal voltage reference as input, the analog comparator should be disabled in all sleep modes. otherwise, the internal voltage ref- erence will be enabled, independent of sleep mode. refer to ?analog comparator? on page 74 for details on how to configure the analog comparator. brown-out detector if the brown-out detector is not needed in the application, this module should be turned off. if the brown-out detector is enabled by the bodlevel fuses, it will be enabled in all sleep modes, and hence, always consume power. in the deeper sleep modes, this will contribute significantly to the total current consumption. refer to ?brown-out detec- tion? on page 32 for details on how to configure the brown-out detector. internal voltage reference the internal voltage reference will be enabled when needed by the brown-out detec- tion, the analog comparator or the adc. if these modules are disabled as described in the sections above, the internal voltage reference will be disabled and it will not be con- suming power. when turned on again, the user must allow the reference to start up before the output is used. if the reference is kept on in sleep mode, the output can be used immediately. refer to ?internal voltage reference? on page 34 for details on the start-up time. watchdog timer if the watchdog timer is not needed in the application, this module should be turned off. if the watchdog timer is enabled, it will be enabled in all sleep modes, and hence, always consume power. in the deeper sleep modes, this will contribute significantly to the total current consumption. refer to ?interrupts? on page 40 for details on how to con- figure the watchdog timer. port pins when entering a sleep mode, all port pins should be configured to use minimum power. the most important thing is then to ensure that no pins drive resistive loads. in sleep modes where both the i/o clock (clk i/o ) and the adc clock (clk adc ) are stopped, the input buffers of the device will be disabled. this ensures that no power is consumed by the input logic when not needed. in some cases, the input logic is needed for detecting wake-up conditions, and it will then be enabled. refer to the section ?digital input enable and sleep modes? on page 45 for details on which pins are enabled. if the input buffer is enabled and the input signal is left floating or has an analog signal level close to v cc /2, the input buffer will use excessive power. for analog input pins, the digital input buffer should be disabled at all times. an analog signal level close to v cc /2 on an input pin can cause significant current even in active mode. digital input buffers can be disabled by writing to the digital input disable regis- ter (didr0). refer to ?digital input disable register 0 ? didr0? on page 76 for details.
29 attiny13 2535b?avr?01/04 system control and reset resetting the avr during reset, all i/o registers are set to their initial values, and the program starts exe- cution from the reset vector. the instruction placed at the reset vector must be a rjmp ? relative jump ? instruction to the reset handling routine. if the program never enables an interrupt source, the interrupt vectors are not used, and regular program code can be placed at these locations. the circuit diagram in figure 13 shows the reset logic. table 12 defines the electrical parameters of the reset circuitry. the i/o ports of the avr are immediately reset to their initial state when a reset source goes active. this does not require any clock source to be running. after all reset sources have gone inactive, a delay counter is invoked, stretching the internal reset. this allows the power to reach a stable level before normal operation starts. the time-out period of the delay c ounter is defined by the user through the sut and cksel fuses. the different selections for the delay period are presented in ?clock sources? on page 21. reset sources the attiny13 has four sources of reset:  power-on reset. the mcu is reset when the supply voltage is below the power-on reset threshold (v pot ).  external reset. the mcu is reset when a low level is present on the reset pin for longer than the minimum pulse length.  watchdog reset. the mcu is reset when the watchdog timer period expires and the watchdog is enabled.  brown-out reset. the mcu is reset when the supply voltage v cc is below the brown-out reset threshold (v bot ) and the brown-out detector is enabled.
30 attiny13 2535b?avr?01/04 figure 13. reset logic notes: 1. values are guidelines only. actual values are tbd. 2. the power-on reset will not work unless the supply voltage has been below v pot (falling) table 12. reset characteristics (1) symbol parameter condition min typ max units v pot power-on reset threshold voltage (rising) t a = -40 - 85 c 1.2 v power-on reset threshold voltage (falling) (2) t a = -40 - 85 c 1.1 v v rst reset pin threshold voltage v cc = 1.8v - 5.5v 0.1 v cc 0.9 v cc v t rst minimum pulse width on reset pin v cc = 1.8v - 5.5v 2.5 s mcu status register (mcusr) brown-out reset circuit bodlevel [1..0] delay counters cksel[1:0] ck timeout wdrf borf extrf porf data bus clock generator spike filter pull-up resistor watchdog oscillator sut [ 1:0 ] power-on reset circuit
31 attiny13 2535b?avr?01/04 power-on reset a power-on reset (por) pulse is generated by an on-chip detection circuit. the detec- tion level is defined in table 12. the por is activated whenever v cc is below the detection level. the por circuit can be used to trigger the start-up reset, as well as to detect a failure in supply voltage. a power-on reset (por) circuit ensures that the device is reset from power-on. reach- ing the power-on reset threshold voltage invokes the delay counter, which determines how long the device is kept in reset after v cc rise. the reset signal is activated again, without any delay, when v cc decreases below the detection level. figure 14. mcu start-up, reset tied to v cc figure 15. mcu start-up, reset extended externally v reset time-out internal reset t tout v pot v rst cc reset time-out internal reset t tout v pot v rst v cc
32 attiny13 2535b?avr?01/04 external reset an external reset is generated by a low level on the reset pin if enabled. reset pulses longer than the minimum pulse width (see table 12) will generate a reset, even if the clock is not running. shorter pulses are not guaranteed to generate a reset. when the applied signal reaches the reset threshold voltage ? v rst ? on its positive edge, the delay counter starts the mcu after the time-out period ? t tout ? has expired. figure 16. external reset during operation brown-out detection attiny13 has an on-chip brown-out detection (bod) circuit for monitoring the v cc level during operation by comparing it to a fixed trigger level. the trigger level for the bod can be selected by the bodlevel fuses. the trigger level has a hysteresis to ensure spike free brown-out detection. the hysteresis on the detection level should be inter- preted as v bot+ = v bot + v hyst /2 and v bot- = v bot - v hyst /2. note: 1. v bot may be below nominal minimum operating voltage for some devices. for devices where this is the case, the device is tested down to v cc = v bot during the production test. this guarantees that a brown-out reset will occur before v cc drops to a voltage where correct operation of the microcontroller is no longer guaranteed. when the bod is enabled, and v cc decreases to a value below the trigger level (v bot- in figure 17), the brown-out reset is immediately activated. when v cc increases above the trigger level (v bot+ in figure 17), the delay counter starts the mcu after the time- out period t tout has expired. the bod circuit will only detect a drop in v cc if the voltage stays below the trigger level for longer than t bod given in table 12. cc table 13. bodlevel fuse coding (1) bodlevel [1..0] fuses min v bot typ v bot max v bot units 11 bod disabled 10 1.8 v 01 2.7 00 4.3 table 14. brown-out characteristics symbol parameter min typ max units v hyst brown-out detector hysteresis 50 mv t bod min pulse width on brown-out reset 2 s
33 attiny13 2535b?avr?01/04 figure 17. brown-out reset during operation watchdog reset when the watchdog times out, it will generate a short reset pulse of one ck cycle dura- tion. on the falling edge of this pulse, the delay timer starts counting the time-out period t tout . refer to page 40 for details on operation of the watchdog timer. figure 18. watchdog reset during operation mcu status register ? mcusr the mcu status register provides information on which reset source caused an mcu reset.  bits 7..4 ? res: reserved bits these bits are reserved bits in the attiny13 and will always read as zero.  bit 3 ? wdrf: watchdog reset flag this bit is set if a watchdog reset occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag.  bit 2 ? borf: brown-out reset flag this bit is set if a brown-out reset occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag.  bit 1 ? extrf: external reset flag v cc reset time-out internal reset v bot- v bot+ t tout ck cc bit 76543210 ? ? ? ? wdrf borf extrf porf mcusr read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 see bit description
34 attiny13 2535b?avr?01/04 this bit is set if an external reset occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag.  bit 0 ? porf: power-on reset flag this bit is set if a power-on reset occurs. the bit is reset only by writing a logic zero to the flag. to make use of the reset flags to identify a reset condition, the user should read and then reset the mcusr as early as possible in the program. if the register is cleared before another reset occurs, the source of the reset can be found by examining the reset flags. internal voltage reference attiny13 features an internal bandgap referenc e. this reference is used for brown-out detection, and it can be used as an input to the analog comparator or the adc. voltage reference enable signals and start-up time the voltage reference has a start-up time that may influence the way it should be used. the start-up time is given in table 15. to save power, the reference is not always turned on. the reference is on during the following situations: 1. when the bod is enabled (by programming the bodlevel [1..0] fuse). 2. when the bandgap reference is connected to the analog comparator (by setting the acbg bit in acsr). 3. when the adc is enabled. thus, when the bod is not enabled, after setting the acbg bit or enabling the adc, the user must always allow the reference to start up before the output from the analog com- parator or adc is used. to reduce power consumption in power-down mode, the user can avoid the three conditions above to ensure that the reference is turned off before entering power-down mode. note: 1. values are guidelines only. actual values are tbd. table 15. internal voltage reference characteristics (1) symbol parameter min typ max units v bg bandgap reference voltage 1.0 1.1 1.2 v t bg bandgap reference start-up time 40 70 s i bg bandgap reference current consumption 15 a
35 attiny13 2535b?avr?01/04 watchdog timer atmega48/88/168 has an enhanced watchdog timer (wdt). the main features are:  clocked from separate on-chip oscillator  3 operating modes ?interrupt ? system reset ? interrupt and system reset  selectable time-out period from 16ms to 8s  possible hardware fuse watchdog always on (wdton) for fail-safe mode figure 19. watchdog timer the watchdog timer (wdt) is a timer counting cycles of a separate on-chip 128 khz oscillator. the wdt gives an interrupt or a system reset when the counter reaches a given time-out value. in normal operation mode, it is required that the system uses the wdr - watchdog timer reset - instruction to restart the counter before the time-out value is reached. if the system doesn't restar t the counter, an interrupt or system reset will be issued. in interrupt mode, the wdt gives an interrupt when the timer expires. this interrupt can be used to wake the device from sleep-modes, and also as a general system timer. one example is to limit the maximum time allowed for certain operations, giving an interrupt when the operation has run longer than expected. in system reset mode, the wdt gives a reset when the timer expires. this is typically used to prevent system hang-up in case of runaway code. the third mode, interrupt and system reset mode, combines the other two modes by first giving an interrupt and then switch to system reset mode. this mode will for instance allow a safe shutdown by saving critical parameters before a sys- tem reset. the watchdog always on (wdton) fuse, if programmed, will force the watchdog timer to system reset mode. with the fuse programmed the system reset mode bit (wde) and interrupt mode bit (wdie) are locked to 1 and 0 respectively. to further ensure pro- gram security, alterations to the watchdog set-up must follow timed sequences. the sequence for clearing wde and changing time-out configuration is as follows: 1. in the same operation, write a logic one to the watchdog change enable bit (wdce) and wde. a logic one must be written to wde regardless of the previ- ous value of the wde bit. 2. within the next four clock cycles, write the wde and watchdog prescaler bits (wdp) as desired, but with the wdce bit cleared. this must be done in one operation. 128khz oscillator osc/2k osc/4k osc/8k osc/16k osc/32k osc/64k osc/128k osc/256k osc/512k osc/1024k wdp0 wdp1 wdp2 wdp3 watchdog reset wde wdif wdie mcu reset interrupt
36 attiny13 2535b?avr?01/04 the following code example shows one assembly and one c function for turning off the watchdog timer. the example assumes that interrupts are controlled (e.g. by disabling interrupts globally) so that no interrupts will occur during the execution of these functions. note: 1. the example code assumes that the part specific header file is included. note: if the watchdog is accidentally enabled, for example by a runaway pointer or brown-out condition, the device will be reset and the watchdog timer will stay enabled. if the code is not set up to handle the watchdog, this might lead to an eternal loop of time-out resets. to avoid this situation, the application software should always clear the assembly code example (1) wdt_off: ; turn off global interrupt cli ; reset watchdog timer wdr ; clear wdrf in mcusr in r16, mcusr andi r16, (0xff & (0< 37 attiny13 2535b?avr?01/04 watchdog system reset flag (wdrf) and the wde control bit in the initialisation rou- tine, even if the watchdog is not in use. the following code example shows one assembly and one c function for changing the time-out value of the watchdog timer. note: 1. the example code assumes that the part specific header file is included. note: the watchdog timer should be reset before any change of the wdp bits, since a change in the wdp bits can result in a time-out when switching to a shorter time-out period. watchdog timer control register - wdtcr assembly code example (1) wdt_prescaler_change: ; turn off global interrupt cli ; reset watchdog timer wdr ; start timed sequence in r16, wdtcr ori r16, (1< 38 attiny13 2535b?avr?01/04  bit 7 - wdif: watchdog interrupt flag this bit is set when a time-out occurs in the watchdog timer and the watchdog timer is configured for interrupt. wdif is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, wdif is cleared by writing a logic one to the flag. when the i-bit in sreg and wdie are set, the watchdog time-out interrupt is executed.  bit 6 - wdie: watchdog interrupt enable when this bit is written to one and the i-bit in the status register is set, the watchdog interrupt is enabled. if wde is cleared in combination with this setting, the watchdog timer is in interrupt mode, and the corresponding interrupt is executed if time-out in the watchdog timer occurs. if wde is set, the watchdog timer is in interrupt and system reset mode. the first time-out in the watchdog timer will set wdif. executing the corresponding interrupt vector will clear wdie and wdif automatically by hardware (the watchdog goes to sys- tem reset mode). this is useful for keeping the watchdog timer security while using the interrupt. to stay in interrupt and system reset mode, wdie must be set after each interrupt. this should however not be done within the interrupt service routine itself, as this might compromise the safety-function of the watchdog system reset mode. if the interrupt is not executed before the next time-out, a system reset will be applied.  bit 4 - wdce: watchdog change enable this bit is used in timed sequences for changing wde and prescaler bits. to clear the wde bit, and/or change the prescaler bits, wdce must be set. once written to one, hardware will clear wdce after four clock cycles.  bit 3 - wde: watchdog system reset enable wde is overridden by wdrf in mcusr. this means that wde is always set when wdrf is set. to clear wde, wdrf must be cleared first. this feature ensures multiple resets during conditions causing failure, and a safe start-up after the failure.  bit 5, 2..0 - wdp3..0: watchdog timer prescaler 3, 2, 1 and 0 the wdp3..0 bits determine the watchdog timer prescaling when the watchdog timer is running. the different prescaling values and their corresponding time-out periods are shown in table 17 on page 39. table 16. watchdog timer configuration wdton wde wdie mode action on time-out 0 0 0 stopped none 0 0 1 interrupt mode interrupt 0 1 0 system reset mode reset 011 interrupt and system reset mode interrupt, then go to system reset mode 1 x x system reset mode reset
39 attiny13 2535b?avr?01/04 . table 17. watchdog timer prescale select wdp3 wdp2 wdp1 wdp0 number of wdt oscillator cycles typical time-out at v cc = 5.0v 0000 2k (2048) cycles 16 ms 0001 4k (4096) cycles 32 ms 0010 8k (8192) cycles 64 ms 0011 16k (16384) cycles 0.125 s 0100 32k (32768) cycles 0.25 s 0101 64k (65536) cycles 0.5 s 0110 128k (131 072) cycles 1.0 s 0111 256k (262 144) cycles 2.0 s 1000 512k (524 288) cycles 4.0 s 10011024k (104 8576) cycles 8.0 s 1010 reserved 1011 1100 1101 1110 1111
40 attiny13 2535b?avr?01/04 interrupts this section describes the specifics of the interrupt handling as performed in attiny13. for a general explanation of the avr interrupt handling, refer to ?reset and interrupt handling? on page 9. interrupt vectors in attiny13 if the program never enables an interrupt source, the interrupt vectors are not used, and regular program code can be placed at these locations. the most typical and general program setup for the reset and interrupt vector addresses in attiny13 is: address labels code comments 0x0000 rjmp reset ; reset handler 0x0001 rjmp ext_int0 ; irq0 handler 0x0002 rjmp pcint0 ; pcint0 handler 0x0003 rjmp tim0_ovf ; timer0 overflow handler 0x0004 rjmp ee_rdy ; eeprom ready handler 0x0005 rjmp ana_comp ; analog comparator handler 0x0006 rjmp tim0_compa ; timer0 comparea handler 0x0007 rjmp tim0_compb ; timer0 compareb handler 0x0008 rjmp watchdog ; watchdog interrupt handler 0x0009 rjmp adc ; adc conversion handler ; 0x000a reset: ldi r16, low(ramend); main program start 0x000b out spl,r16 ; set stack pointer to top of ram 0x000c sei ; enable interrupts 0x000d xxx ... ... ... ... table 18. reset and interrupt vectors vector no. program address source interrupt definition 1 0x0000 r eset external pin, power-on reset, brown-out reset, watchdog reset 2 0x0001 int0 external interrupt request 0 3 0x0002 pcint0 pin change interrupt request 0 4 0x0003 tim0_ovf timer/counter overflow 5 0x0004 ee_rdy eeprom ready 6 0x0005 ana_comp analog comparator 7 0x0006 tim0_compa timer/counter compare match a 8 0x0007 tim0_compb timer/counter compare match b 9 0x0008 wdt watchdog time-out 10 0x0009 adc adc conversion complete
41 attiny13 2535b?avr?01/04 i/o ports introduction all avr ports have true read-modify-write functionality when used as general digital i/o ports. this means that the direction of one port pin can be changed without uninten- tionally changing the direction of any other pin with the sbi and cbi instructions. the same applies when changing drive value (if configured as output) or enabling/disabling of pull-up resistors (if configured as input). each output buffer has symmetrical drive characteristics with both high sink and source capability. the pin driver is strong enough to drive led displays directly. all port pins have individually selectable pull-up resistors with a supply-voltage invariant resistance. all i/o pins have protection diodes to both v cc and ground as indicated in figure 20. refer to ?electrical characteristics? on page 115 for a complete list of parameters. figure 20. i/o pin equivalent schematic all registers and bit references in this sect ion are written in general form. a lower case ?x? represents the numbering letter for the por t, and a lower case ?n? represents the bit number. however, when using the register or bit defines in a program, the precise form must be used. for example, portb3 for bit no. 3 in port b, here documented generally as portxn. the physical i/o registers and bit locations are listed in ?register descrip- tion for i/o-ports? on page 51. three i/o memory address locations are allocated for each port, one each for the data register ? portx, data direction register ? ddrx, and the port input pins ? pinx. the port input pins i/o location is read only, while the data register and the data direction register are read/write. however, writing a logic one to a bit in the pinx register, will result in a toggle in the corresponding bit in the data register. in addition, the pull-up disable ? pud bit in mcucr disables the pull-up function for all pins in all ports when set. using the i/o port as general digital i/o is described in ?ports as general digital i/o? on page 42. most port pins are multiplexed with alternate functions for the peripheral fea- tures on the device. how each alternate function interferes with the port pin is described in ?alternate port functions? on page 46. refer to the individual module sections for a full description of the alternate functions. c pin logic r pu see figure "general digital i/o" for details pxn
42 attiny13 2535b?avr?01/04 note that enabling the alternate function of some of the port pins does not affect the use of the other pins in the port as general digital i/o. ports as general digital i/o the ports are bi-directional i/o ports with optional internal pull-ups. figure 21 shows a functional description of one i/o-port pin, here generically called pxn. figure 21. general digital i/o (1) note: 1. wrx, wpx, wdx, rrx, rpx, and rdx are common to all pins within the same port. clk i/o , sleep, and pud are common to all ports. configuring the pin each port pin consists of three register bits: ddxn, portxn, and pinxn. as shown in ?register description for i/o-ports? on page 51, the ddxn bits are accessed at the ddrx i/o address, the portxn bits at the portx i/o address, and the pinxn bits at the pinx i/o address. the ddxn bit in the ddrx register selects the direction of this pin. if ddxn is written logic one, pxn is configured as an output pin. if ddxn is written logic zero, pxn is config- ured as an input pin. if portxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated. to switch the pull-up resistor off, portxn has to be written logic zero or the pin has to be configured as an output pin. the port pins are tri-stated when reset condition becomes active, even if no clocks are running. if portxn is written logic one when the pin is configured as an output pin, the port pin is driven high (one). if portxn is written logic zero when the pin is configured as an out- put pin, the port pin is driven low (zero). clk rpx rrx rdx wdx pud synchronizer wdx: write ddrx wrx: write portx rrx: read portx register rpx: read portx pin pud: pullup disable clk i/o : i/o clock rdx: read ddrx d l q q reset reset q q d q q d clr portxn q q d clr ddxn pinxn dat a bus sleep sleep: sleep control pxn i/o wpx 0 1 wrx wpx: write pinx register
43 attiny13 2535b?avr?01/04 toggling the pin writing a logic one to pinxn toggles the value of portxn, independent on the value of ddrxn. note that the sbi instruction can be used to toggle one single bit in a port. switching between input and output when switching between tri-state ({ddxn, portxn} = 0b00) and output high ({ddxn, portxn} = 0b11), an intermediate state with either pull-up enabled {ddxn, portxn} = 0b01) or output low ({ddxn, portxn} = 0b10) must occur. normally, the pull-up enabled state is fully acceptable, as a high-impedant environment will not notice the dif- ference between a strong high driver and a pull-up. if this is not the case, the pud bit in the mcucr register can be set to disable all pull-ups in all ports. switching between input with pull-up and output low generates the same problem. the user must use either the tri-state ({ddxn, portxn} = 0b00) or the output high state ({ddxn, portxn} = 0b10) as an intermediate step. table 19 summarizes the control signals for the pin value. reading the pin value independent of the setting of data direction bit ddxn, the port pin can be read through the pinxn register bit. as shown in figure 21, the pinxn register bit and the preceding latch constitute a synchronizer. this is needed to avoid metastability if the physical pin changes value near the edge of the internal clock, but it also introduces a delay. figure 22 shows a timing diagram of the synchronization when reading an externally applied pin value. the maximum and minimum propagation delays are denoted t pd,max and t pd,min respectively. figure 22. synchronization when reading an externally applied pin value table 19. port pin configurations ddxn portxn pud (in mcucr) i/o pull-up comment 0 0 x input no tri-state (hi-z) 0 1 0 input yes pxn will source current if ext. pulled low. 0 1 1 input no tri-state (hi-z) 1 0 x output no output low (sink) 1 1 x output no output high (source) xxx in r17, pinx 0x00 0xff instructions sync latch pinxn r17 xxx system clk t pd, max t pd, min
44 attiny13 2535b?avr?01/04 consider the clock period starting shortly after the first falling edge of the system clock. the latch is closed when the clock is low, and goes transparent when the clock is high, as indicated by the shaded region of the ?sync latch? signal. the signal value is latched when the system clock goes low. it is clocked into the pinxn register at the suc- ceeding positive clock edge. as indicated by the two arrows tpd,max and tpd,min, a single signal transition on the pin will be delayed between ? and 1? system clock period depending upon the time of assertion. when reading back a software assigned pin value, a nop instruction must be inserted as indicated in figure 23. the out instruction sets the ?sync latch? signal at the positive edge of the clock. in this case, the delay tpd through the synchronizer is one system clock period. figure 23. synchronization when reading a software assigned pin value out portx, r16 nop in r17, pinx 0xff 0x00 0xff system clk r16 instructions sync latch pinxn r17 t pd
45 attiny13 2535b?avr?01/04 the following code example shows how to set port b pins 0 and 1 high, 2 and 3 low, and define the port pins from 4 to 5 as input with a pull-up assigned to port pin 4. the result- ing pin values are read back again, but as previously discussed, a nop instruction is included to be able to read back the value recently assigned to some of the pins. note: 1. for the assembly program, two temporary registers are used to minimize the time from pull-ups are set on pins 0, 1 and 4, until the direction bits are correctly set, defin- ing bit 2 and 3 as low and redefining bits 0 and 1 as strong high drivers. digital input enable and sleep modes as shown in figure 21, the digital input signal can be clamped to ground at the input of the schmitt-trigger. the signal denoted sleep in the figure, is set by the mcu sleep controller in power-down mode, power-save mode, and standby mode to avoid high power consumption if some input signals are left floating, or have an analog signal level close to v cc /2. sleep is overridden for port pins enabled as external interrupt pins. if the external inter- rupt request is not enabled, sleep is active also for these pins. sleep is also overridden by various other alternate functions as described in ?alternate port func- tions? on page 46. if a logic high level (?one?) is present on an asynchronous external interrupt pin config- ured as ?interrupt on rising edge, falling edge, or any logic change on pin? while the external interrupt is not enabled, the corresponding external interrupt flag will be set assembly code example (1) ... ; define pull-ups and set outputs high ; define directions for port pins ldi r16,(1< 46 attiny13 2535b?avr?01/04 when resuming from the above mentioned sleep mode, as the clamping in these sleep mode produces the requested logic change. unconnected pins if some pins are unused, it is recommended to ensure that these pins have a defined level. even though most of the digital inputs are disabled in the deep sleep modes as described above, floating inputs should be avoided to reduce current consumption in all other modes where the digital inputs are enabled (reset, active mode and idle mode). the simplest method to ensure a defined level of an unused pin, is to enable the internal pull-up. in this case, the pull-up will be disabled during reset. if low power consumption during reset is important, it is recommended to use an external pull-up or pull-down. connecting unused pins directly to v cc or gnd is not recommended, since this may cause excessive currents if the pin is accidentally configured as an output. alternate port functions most port pins have alternate functions in addition to being general digital i/os. figure 24 shows how the port pin control signals from the simplified figure 21 can be overrid- den by alternate functions. the overriding signals may not be present in all port pins, but the figure serves as a generic description applicable to all port pins in the avr micro- controller family.
47 attiny13 2535b?avr?01/04 figure 24. alternate port functions (1) note: 1. wrx, wpx, wdx, rrx, rpx, and rdx are common to all pins within the same port. clk i/o , sleep, and pud are common to all ports. all other signals are unique for each pin. clk rpx rrx wrx rdx wdx pud synchronizer wdx: write ddrx wrx: write portx rrx: read portx register rpx: read portx pin pud: pullup disable clk i/o : i/o clock rdx: read ddrx d l q q set clr 0 1 0 1 0 1 dixn aioxn dieoexn pvovxn pvoexn ddovxn ddoexn puoexn puovxn puoexn: pxn pull-up override enable puovxn: pxn pull-up override value ddoexn: pxn data direction override enable ddovxn: pxn data direction override value pvoexn: pxn port value override enable pvovxn: pxn port value override value dixn: digital input pin n on portx aioxn: analog input/output pin n on portx reset reset q q d clr q q d clr q q d clr pinxn portxn ddxn data b u s 0 1 dieovxn sleep dieoexn: pxn digital input-enable override enable dieovxn: pxn digital input-enable override value sleep: sleep control pxn i/o 0 1 ptoexn ptoexn: pxn, port toggle override enable wpx: write pinx wpx
48 attiny13 2535b?avr?01/04 table 20 summarizes the function of the overriding signals. the pin and port indexes from figure 24 are not shown in the succeeding tables. the overriding signals are gen- erated internally in the modules having the alternate function. the following subsections shortly describe the alternate functions for each port, and relate the overriding signals to the alternate function. refer to the alternate function description for further details. table 20. generic description of overriding signals for alternate functions signal name full name description puoe pull-up override enable if this signal is set, the pull-up enable is controlled by the puov signal. if this signal is cleared, the pull-up is enabled when {ddxn, portxn, pud} = 0b010. puov pull-up override value if puoe is set, the pull-up is enabled/disabled when puov is set/cleared, regardless of the setting of the ddxn, portxn, and pud register bits. ddoe data direction override enable if this signal is set, the output driver enable is controlled by the ddov signal. if this signal is cleared, the output driver is enabled by the ddxn register bit. ddov data direction override value if ddoe is set, the output driver is enabled/disabled when ddov is set/cleared, regardless of the setting of the ddxn register bit. pvoe port value override enable if this signal is set and the output driver is enabled, the port value is controlled by the pvov signal. if pvoe is cleared, and the output driver is enabled, the port value is controlled by the portxn register bit. pvov port value override value if pvoe is set, the port value is set to pvov, regardless of the setting of the portxn register bit. ptoe port toggle override enable if ptoe is set, the portxn register bit is inverted. dieoe digital input enable override enable if this bit is set, the digital input enable is controlled by the dieov signal. if this signal is cleared, the digital input enable is determined by mcu state (normal mode, sleep mode). dieov digital input enable override value if dieoe is set, the digital input is enabled/disabled when dieov is set/cleared, regardless of the mcu state (normal mode, sleep mode). di digital input this is the digital input to alternate functions. in the figure, the signal is connected to the output of the schmitt-trigger but before the synchronizer. unless the digital input is used as a clock source, the module with the alternate function will use its own synchronizer. aio analog input/output this is the analog input/output to/from alternate functions. the signal is connected directly to the pad, and can be used bi-directionally.
49 attiny13 2535b?avr?01/04 mcu control register ? mcucr  bits 7, 2? res: reserved bits these bits are reserved bits in the attiny13 and will always read as zero.  bit 6 ? pud: pull-up disable when this bit is written to one, the pull-ups in the i/o ports are disabled even if the ddxn and portxn registers are configured to enable the pull-ups ({ddxn, portxn} = 0b01). see ?configuring the pin? on page 42 for more details about this feature. alternate functions of port b the port b pins with alternate function are shown in table 21. notes: 1. reset pin, debugwire i/o, adc input channel, or pin change interrupt. 2. adc input channel or pin change interrupt. 3. adc input channel, clock input, or pin change interrupt. 4. serial clock input, timer/counter clock input, adc input channel 0, or pin change interrupt. 5. serial data input, analog comparator negative input, output compare and pwm output b for timer/counter, external interrupt 0 or pin change interrupt. 6. serial data output, analog comparator positive input, output compare and pwm output a for timer/counter, or pin change interrupt. table 22 and table 23 on page 50 relate the alternate functions of port b to the overrid- ing signals shown in figure 24 on page 47. bit 7 6 5 4 3 2 1 0 ?pud se sm1 sm0 ? isc01 isc00 mcucr read/write r r/w r/w r/w r/w r r/w r/w initial value 0 0 0 0 0 0 0 0 table 21. port b pins alternate functions port pin alternate function pb5 reset /dw/adc0/pcint5 (1) pb4 adc2/pcint4 (2) pb3 adc3/clki/pcint3 (3) pb2 sck/adc1/t0/pcint2 (4) pb1 miso/ain1/oc0b/int0/pcint1/rxd (5) pb0 mosi/ain0/oc0a/pcint0/txd (6)
50 attiny13 2535b?avr?01/04 note: 1. 1 when the fuse is ?0? (programmed). table 22. overriding signals for alternate functions in pb5..pb3 signal name pb5/reset/ adc0/pcint5 pb4/adc2/pcint4 pb3/adc3/clki/pcint3 puoe rstdisbl (1)  dwen (1) 00 puov 1 0 0 ddoe rstdisbl (1)  dwen (1) 00 ddov debugwire transmit 0 0 pvoe 0 0 0 pvov 0 0 0 ptoe 0 0 0 dieoe rstdisbl (1) + (pcint5  pcie + adc0d) pcint4  pcie + adc2d pcint3  pcie + adc3d dieov adc0d adc2d adc3d di pcint5 input pcint4 input pcint3 input aio reset input, adc0 input adc2 input adc3 input table 23. overriding signals for alternate functions in pb2..pb0 signal name pb2/sck/adc1/ t0/pcint2 pb1/miso/ain1/ oc0b/int0/pcint1 pb0/mosi/ain0/aref/ oc0a/pcint0 puoe 0 0 0 puov 0 0 0 ddoe 0 0 0 ddov 0 0 0 pvoe 0 oc0b enable oc0a enable pvov 0 oc0b oc0a ptoe 0 0 0 dieoe pcint2  pcie + adc1d pcint1  pcie + ain1d pcint0  pcie + ain0d dieov adc1d ain1d ain0d di t0/int0/ pcint2 input pcint1 input pcint0 input aio adc1 input analog comparator negative input analog comparator positive input
51 attiny13 2535b?avr?01/04 register description for i/o-ports port b data register ? portb port b data direction register ? ddrb port b input pins address ? pinb bit 76543210 ? ? portb5 portb4 portb3 portb2 portb1 portb0 portb read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 ?? ddb5 ddb4 ddb3 ddb2 ddb1 ddb0 ddrb read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 ?? pinb5 pinb4 pinb3 pinb2 pinb1 pinb0 pinb read/write r r r/w r/w r/w r/w r/w r/w initial value 0 0 n/a n/a n/a n/a n/a n/a
52 attiny13 2535b?avr?01/04 external interrupts the external interrupts are triggered by the int0 pin or any of the pcint5..0 pins. observe that, if enabled, the interrupts will trigger even if the int0 or pcint5..0 pins are configured as outputs. this feature provides a way of generating a software interrupt. pin change interrupts pci will trigger if any enabled pcint5..0 pin toggles. the pcmsk register control which pins contribute to the pin change interrupts. pin change interrupts on pcint5..0 are detected asynchronously. this implies that these interrupts can be used for waking the part also from sleep modes other than idle mode. the int0 interrupts can be triggered by a falling or rising edge or a low level. this is set up as indicated in the specification for the mcu control register ? mcucr. when the int0 interrupt is enabled and is configured as level triggered, the interrupt will trigger as long as the pin is held low. note that recognition of falling or rising edge interrupts on int0 requires the presence of an i/o clock, described in ?clock systems and their distri- bution? on page 20. low level interrupt on int0 is detected asynchronously. this implies that this interrupt can be used for waking the part also from sleep modes other than idle mode. the i/o clock is halted in all sleep modes except idle mode. note that if a level triggered interrupt is used for wake-up from power-down, the required level must be held long enough for the mcu to complete the wake-up to trigger the level interrupt. if the level disappears before the end of the start-up time, the mcu will still wake up, but no interrupt will be generated. the start-up time is defined by the sut and cksel fuses as described in ?system clock and clock options? on page 20. mcu control register ? mcucr the external interrupt control register a contains control bits for interrupt sense control.  bits 1, 0 ? isc01, isc00: interrupt sense control 0 bit 1 and bit 0 the external interrupt 0 is activated by the external pin int0 if the sreg i-flag and the corresponding interrupt mask are set. the level and edges on the external int0 pin that activate the interrupt are defined in table 24. the value on the int0 pin is sampled before detecting edges. if edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. shorter pulses are not guaranteed to generate an interrupt. if low level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt. bit 76543210 ? pud se sm1 sm0 ? isc01 isc00 mcucr read/write r r/w r/w r/w r/w r r/w r/w initial value00000000 table 24. interrupt 0 sense control isc01 isc00 description 0 0 the low level of int0 generates an interrupt request. 0 1 any logical change on int0 generates an interrupt request. 1 0 the falling edge of int0 generates an interrupt request. 1 1 the rising edge of int0 generates an interrupt request.
53 attiny13 2535b?avr?01/04 general interrupt mask register ? gimsk  bits 7, 4..0 ? res: reserved bits these bits are reserved bits in the attiny13 and will always read as zero.  bit 6 ? int0: external interrupt request 0 enable when the int0 bit is set (one) and the i-bit in the status register (sreg) is set (one), the external pin interrupt is enabled. the interrupt sense control0 bits 1/0 (isc01 and isc00) in the external interrupt control register a (eicra) define whether the external interrupt is activated on rising and/or falling edge of the int0 pin or level sensed. activity on the pin will cause an interrupt request even if int0 is configured as an output. the corresponding interrupt of external interrupt request 0 is executed from the int0 inter- rupt vector.  bit 5 ? pcie: pin change interrupt enable when the pcie bit is set (one) and the i-bit in the status register (sreg) is set (one), pin change interrupt is enabled. any change on any enabled pcint5..0 pin will cause an interrupt. the corresponding interrupt of pin change interrupt request is executed from the pci interrupt vector. pcint5..0 pins are enabled individually by the pcmsk0 register. general interrupt flag register ? gifr  bits 7, 4..0 ? res: reserved bits these bits are reserved bits in the attiny13 and will always read as zero.  bit 6 ? intf0: external interrupt flag 0 when an edge or logic change on the int0 pin triggers an interrupt request, intf0 becomes set (one). if the i-bit in sreg and the int0 bit in gimsk are set (one), the mcu will jump to the corresponding interrupt vector. the flag is cleared when the inter- rupt routine is executed. alternatively, the flag can be cleared by writing a logical one to it. this flag is always cleared when int0 is configured as a level interrupt.  bit 5 ? pcif: pin change interrupt flag when a logic change on any pcint5..0 pin triggers an interrupt request, pcif becomes set (one). if the i-bit in sreg and the pcie bit in gimsk are set (one), the mcu will jump to the corresponding interrupt vector. the flag is cleared when the interrupt routine is executed. alternatively, the flag can be cleared by writing a logical one to it. bit 76543210 ? int0 pcie ? ? ? ? ? gimsk read/writerr/wr/wrrrrr initial value00000000 bit 76543210 ?intf0pcif?????gifr read/writerr/wr/wrrrrr initial value00000000
54 attiny13 2535b?avr?01/04 pin change mask register ? pcmsk  bits 7, 6 ? res: reserved bits these bits are reserved bits in the attiny13 and will always read as zero.  bits 5..0 ? pcint5..0: pin change enable mask 5..0 each pcint5..0 bit selects whether pin change interrupt is enabled on the correspond- ing i/o pin. if pcint5..0 is set and the pcie bit in gimsk is set, pin change interrupt is enabled on the corresponding i/o pin. if pcint5..0 is cleared, pin change interrupt on the corresponding i/o pin is disabled. bit 76543210 ? ? pcint5 pcint4 pcint3 pcint2 pcint1 pcint0 pcmsk read/write r r r/w r/w r/w r/w r/w r/w initial value00111111
55 attiny13 2535b?avr?01/04 8-bit timer/counter0 with pwm timer/counter0 is a general purpose 8-bit timer/counter module, with two independent output compare units, and with pwm support. it allows accurate program execution timing (event management) and wave generation. the main features are:  two independent output compare units  double buffered output compare registers  clear timer on compare match (auto reload)  glitch free, phase correct pulse width modulator (pwm)  variable pwm period  frequency generator  three independent interrupt sources (tov0, ocf0a, and ocf0b) overview a simplified block diagram of the 8-bit timer/counter is shown in figure 25. for the actual placement of i/o pins, refer to ?pinout attiny13? on page 1. cpu accessible i/o registers, including i/o bits and i/o pins, are shown in bold. the device-specific i/o register and bit locations are listed in the ?8-bit timer/counter register description? on page 66. figure 25. 8-bit timer/counter block diagram registers the timer/counter (tcnt0) and output compare registers (ocr0a and ocr0b) are 8-bit registers. interrupt request (abbreviated to int.req. in the figure) signals are all vis- ible in the timer interrupt flag register (tifr0). all interrupts are individually masked with the timer interrupt mask register (timsk0). tifr0 and timsk0 are not shown in the figure. the timer/counter can be clocked internally, vi a the prescaler, or by an external clock source on the t0 pin. the clock select logic block controls which clock source and edge the timer/counter uses to increment (or decrement) its value. the timer/counter is inactive when no clock source is selected. the output from the clock select logic is referred to as the timer clock (clk t0 ). the double buffered output compare registers (ocr0a and ocr0b) is compared with the timer/counter value at all times. the result of the compare can be used by the waveform generator to generate a pwm or variable frequency output on the output clock select timer/counter data bus ocrna ocrnb = = tcntn waveform generation waveform generation ocna ocnb = fixed top value control logic = 0 top bottom count clear direction tovn (int.req.) ocna (int.req.) ocnb (int.req.) tccrna tccrnb tn edge detector ( from prescaler ) clk tn
56 attiny13 2535b?avr?01/04 compare pins (oc0a and oc0b). see ?output compare unit? on page 57. for details. the compare match event will also set the compare flag (ocf0a or ocf0b) which can be used to generate an output compare interrupt request. definitions many register and bit references in this section are written in general form. a lower case ?n? replaces the timer/counter number, in this case 0. a lower case ?x? replaces the output compare unit, in this case compare unit a or compare unit b. however, when using the register or bit defines in a program, the precise form must be used, i.e., tcnt0 for accessing timer/counter0 counter value and so on. the definitions in table 25 are also used extensively throughout the document. timer/counter clock sources the timer/counter can be clocked by an internal or an external clock source. the clock source is selected by the clock select logic which is controlled by the clock select (cs02:0) bits located in the timer/counter control register (tccr0b). for details on clock sources and prescaler, see ?timer/counter prescaler? on page 72. counter unit the main part of the 8-bit timer/counter is the programmable bi-directional counter unit. figure 26 shows a block diagram of the counter and its surroundings. figure 26. counter unit block diagram signal description (internal signals): count increment or decrement tcnt0 by 1. direction select between increment and decrement. clear clear tcnt0 (set all bits to zero). clk t n timer/counter clock, referred to as clk t0 in the following. top signalize that tcnt0 has reached maximum value. bottom signalize that tcnt0 has reached minimum value (zero). table 25. definitions bottom the counter reaches the bottom when it becomes 0x00. max the counter reaches its maximum when it becomes 0xff (decimal 255). top the counter reaches the top when it becomes equal to the highest value in the count sequence. the top value can be assigned to be the fixed value 0xff (max) or the value stored in the ocr0a register. the assignment is dependent on the mode of operation. data b u s tcntn control logic count tovn (int.req.) clock select top tn edge detector ( from prescaler ) clk tn bottom direction clear
57 attiny13 2535b?avr?01/04 depending of the mode of operation used, the counter is cleared, incremented, or dec- remented at each timer clock (clk t0 ). clk t0 can be generated from an external or internal clock source, selected by the clock select bits (cs02:0). when no clock source is selected (cs02:0 = 0) the timer is stopped. however, the tcnt0 value can be accessed by the cpu, regardless of whether clk t0 is present or not. a cpu write overrides (has priority over) all counter clear or count operations. the counting sequence is determined by the setting of the wgm01 and wgm00 bits located in the timer/counter control register (tccr0a) and the wgm02 bit located in the timer/counter control register b (tccr0b). there are close connections between how the counter behaves (counts) and how waveforms are generated on the output compare output oc0a. for more details about advanced counting sequences and waveform generation, see ?modes of operation? on page 60. the timer/counter overflow flag (tov0) is set according to the mode of operation selected by the wgm01:0 bits. tov0 can be used for generating a cpu interrupt. output compare unit the 8-bit comparator continuously compares tcnt0 with the output compare regis- ters (ocr0a and ocr0b). whenever tcnt0 equals ocr0a or ocr0b, the comparator signals a match. a match will set the output compare flag (ocf0a or ocf0b) at the next timer clock cycle. if the corresponding interrupt is enabled, the out- put compare flag generates an output compare interrupt. the output compare flag is automatically cleared when the interrupt is executed. alternatively, the flag can be cleared by software by writing a logical one to its i/o bit location. the waveform gener- ator uses the match signal to generate an output according to operating mode set by the wgm02:0 bits and compare output mode (com0x1:0) bits. the max and bottom sig- nals are used by the waveform generator for handling the special cases of the extreme values in some modes of operation (see ?modes of operation? on page 60.). figure 27 shows a block diagram of the output compare unit. figure 27. output compare unit, block diagram ocfn x (int.req.) = (8-bit comparator ) ocrnx ocnx data b u s tcntn wgmn1:0 waveform generator top focn comnx1:0 bottom
58 attiny13 2535b?avr?01/04 the ocr0x registers are double buffered when using any of the pulse width modula- tion (pwm) modes. for the normal and clear timer on compare (ctc) modes of operation, the double buffering is disabled. the double buffering synchronizes the update of the ocr0x compare registers to either top or bottom of the counting sequence. the synchronization prevents the occurrence of odd-length, non-symmetrical pwm pulses, thereby making the output glitch-free. the ocr0x register access may seem complex, but this is not case. when the double buffering is enabled, the cpu has access to the ocr0x buffer register, and if double buffering is disabled the cpu will access the ocr0x directly. force output compare in non-pwm waveform generation modes, the match output of the comparator can be forced by writing a one to the force output compare (foc0x) bit. forcing compare match will not set the ocf0x flag or reload/clear the timer, but the oc0x pin will be updated as if a real compare match had occurred (the com0x1:0 bits settings define whether the oc0x pin is set, cleared or toggled). compare match blocking by tcnt0 write all cpu write operations to the tcnt0 register will block any compare match that occur in the next timer clock cycle, even when the timer is stopped. this feature allows ocr0x to be initialized to the same value as tcnt0 without triggering an interrupt when the timer/counter clock is enabled. using the output compare unit since writing tcnt0 in any mode of operation will block all compare matches for one timer clock cycle, there are risks involved when changing tcnt0 when using the output compare unit, independently of whether the timer/counter is running or not. if the value written to tcnt0 equals the ocr0x value, the compare match will be missed, resulting in incorrect waveform generation. similarly, do not write the tcnt0 value equal to bottom when the counter is down-counting. the setup of the oc0x should be performed before setting the data direction register for the port pin to output. the easiest way of setting the oc0x value is to use the force output compare (foc0x) strobe bits in normal mode. the oc0x registers keep their values even when changing between waveform generation modes. be aware that the com0x1:0 bits are not double buffered together with the compare value. changing the com0x1:0 bits will take effect immediately.
59 attiny13 2535b?avr?01/04 compare match output unit the compare output mode (com0x1:0) bits have two functions. the waveform gener- ator uses the com0x1:0 bits for defining the output compare (oc0x) state at the next compare match. also, the com0x1:0 bits control the oc0x pin output source. figure 28 shows a simplified schematic of the logic affected by the com0x1:0 bit setting. the i/o registers, i/o bits, and i/o pins in the figure are shown in bold. only the parts of the general i/o port control registers (ddr and port) that are affected by the com0x1:0 bits are shown. when referring to the oc0x state, the reference is for the internal oc0x register, not the oc0x pin. if a system reset occur, the oc0x register is reset to ?0?. figure 28. compare match output unit, schematic the general i/o port function is overridden by the output compare (oc0x) from the waveform generator if either of the com0x1:0 bits are set. however, the oc0x pin direction (input or output) is still controlled by the data direction register (ddr) for the port pin. the data direction register bit for the oc0x pin (ddr_oc0x) must be set as output before the oc0x value is visible on the pin. the port override function is indepen- dent of the waveform generation mode. the design of the output compare pin logic allows initialization of the oc0x state before the output is enabled. note that some com0x1:0 bit settings are reserved for certain modes of operation. see ?8-bit timer/counter register description? on page 66. compare output mode and waveform generation the waveform generator uses the com0x1:0 bits differently in normal, ctc, and pwm modes. for all modes, setting the com0x1:0 = 0 tells the waveform generator that no action on the oc0x register is to be performed on the next compare match. for com- pare output actions in the non-pwm modes refer to table 26 on page 66. for fast pwm mode, refer to table 27 on page 66, and for phase correct pwm refer to table 28 on page 67. a change of the com0x1:0 bits state will have effect at the first compare match after the bits are written. for non-pwm modes, the action can be forced to have immediate effect by using the foc0x strobe bits. port ddr dq dq ocn pin ocnx dq waveform generator comnx1 comnx0 0 1 data b u s focn clk i/o
60 attiny13 2535b?avr?01/04 modes of operation the mode of operation, i.e., the behavior of the timer/counter and the output compare pins, is defined by the combination of the waveform generation mode (wgm02:0) and compare output mode (com0x1:0) bits. the compare output mode bits do not affect the counting sequence, while the waveform generation mode bits do. the com0x1:0 bits control whether the pwm output generated should be inverted or not (inverted or non-inverted pwm). for non-pwm modes the com0x1:0 bits control whether the out- put should be set, cleared, or toggled at a compare match (see ?compare match output unit? on page 59.). for detailed timing information refer to figure 32, figure 33, figure 34 and figure 35 in ?timer/counter timing diagrams? on page 64. normal mode the simplest mode of operation is the normal mode (wgm02:0 = 0). in this mode the counting direction is always up (incrementing), and no counter clear is performed. the counter simply overruns when it passes its maximum 8-bit value (top = 0xff) and then restarts from the bottom (0x00). in normal operation the timer/counter overflow flag (tov0) will be set in the same timer clock cycle as the tcnt0 becomes zero. the tov0 flag in this case behaves like a ninth bit, except that it is only set, not cleared. however, combined with the timer overflow interrupt that automatically clears the tov0 flag, the timer resolution can be increased by software. there are no special cases to consider in the normal mode, a new counter value can be written anytime. the output compare unit can be used to generate interrupts at some given time. using the output compare to generate waveforms in normal mode is not recommended, since this will occupy too much of the cpu time. clear timer on compare match (ctc) mode in clear timer on compare or ctc mode (wgm02:0 = 2), the ocr0a register is used to manipulate the counter resolution. in ctc mode the counter is cleared to zero when the counter value (tcnt0) matches the ocr0a. the ocr0a defines the top value for the counter, hence also its resolution. this mode allows greater control of the compare match output frequency. it also simplifies the operation of counting external events. the timing diagram for the ctc mode is shown in figure 29. the counter value (tcnt0) increases until a compare match occurs between tcnt0 and ocr0a, and then counter (tcnt0) is cleared. figure 29. ctc mode, timing diagram an interrupt can be generated each time the counter value reaches the top value by using the ocf0a flag. if the interrupt is enabled, the interrupt handler routine can be used for updating the top value. however, changing top to a value close to bottom when the counter is running with none or a low prescaler value must be done with care since the ctc mode does not have the double buffering feature. if the new value written tcntn ocn (toggle) ocnx interrupt flag set 1 4 period 2 3 (comnx1:0 = 1)
61 attiny13 2535b?avr?01/04 to ocr0a is lower than the current value of tcnt0, the counter will miss the compare match. the counter will then have to count to its maximum value (0xff) and wrap around starting at 0x00 before the compare match can occur. for generating a waveform output in ctc mode, the oc0a output can be set to toggle its logical level on each compare match by setting the compare output mode bits to toggle mode (com0a1:0 = 1). the oc0a value will not be visible on the port pin unless the data direction for the pin is set to output. the waveform generated will have a maxi- mum frequency of f oc0 = f clk_i/o /2 when ocr0a is set to zero (0x00). the waveform frequency is defined by the following equation: the n variable represents the prescale factor (1, 8, 64, 256, or 1024). as for the normal mode of operation, the tov0 flag is set in the same timer clock cycle that the counter counts from max to 0x00. fast pwm mode the fast pulse width modulation or fast pwm mode (wgm02:0 = 3 or 7) provides a high frequency pwm waveform generation option. the fast pwm differs from the other pwm option by its single-slope operation. the counter counts from bottom to top then restarts from bottom. top is defined as 0xff when wgm2:0 = 3, and ocr0a when wgm2:0 = 7. in non-inverting compare output mode, the output compare (oc0x) is cleared on the compare match between tcnt0 and ocr0x, and set at bottom. in inverting compare output mode, the output is set on compare match and cleared at bottom. due to the single-slope operation, the operating frequency of the fast pwm mode can be twice as high as the phase correct pwm mode that use dual-slope opera- tion. this high frequency makes the fast pwm mode well suited for power regulation, rectification, and dac applications. high frequency allows physically small sized exter- nal components (coils, capacitors), and therefore reduces total system cost. in fast pwm mode, the counter is incremented until the counter value matches the top value. the counter is then cleared at the following timer clock cycle. the timing diagram for the fast pwm mode is shown in figure 30. the tcnt0 value is in the timing diagram shown as a histogram for illustrating the single-slope operation. the diagram includes non-inverted and inverted pwm outputs. the small horizontal line marks on the tcnt0 slopes represent compare matches between ocr0x and tcnt0. f ocnx f clk_i/o 2 n 1 ocrnx + () ?? ------------------------------------------------- - =
62 attiny13 2535b?avr?01/04 figure 30. fast pwm mode, timing diagram the timer/counter overflow flag (tov0) is set each time the counter reaches top. if the interrupt is enabled, the interrupt handler routine can be used for updating the com- pare value. in fast pwm mode, the compare unit allows generation of pwm waveforms on the oc0x pins. setting the com0x1:0 bits to two will produce a non-inverted pwm and an inverted pwm output can be generated by setting the com0x1:0 to three: setting the com0a1:0 bits to one allows the ac0a pin to toggle on compare matches if the wgm02 bit is set. this option is not available for the oc0b pin (see table 27 on page 66). the actual oc0x value will only be visible on the port pin if the data direction for the port pin is set as output. the pwm waveform is generated by setting (or clearing) the oc0x register at the compare match between ocr0x and tcnt0, and clearing (or setting) the oc0x register at the timer clock cycle the counter is cleared (changes from top to bottom). the pwm frequency for the output can be calculated by the following equation: the n variable represents the prescale factor (1, 8, 64, 256, or 1024). the extreme values for the ocr0a register represents special cases when generating a pwm waveform output in the fast pwm mode. if the ocr0a is set equal to bottom, the output will be a narrow spike for each max+1 timer clock cycle. setting the ocr0a equal to max will result in a constantly high or low output (depending on the polarity of the output set by the com0a1:0 bits.) a frequency (with 50% duty cycle) waveform output in fast pwm mode can be achieved by setting oc0x to toggle its logical level on each compare match (com0x1:0 = 1). the waveform generated will have a maximum frequency of f oc0 = f clk_i/o /2 when ocr0a is set to zero. this feature is similar to the oc0a toggle in ctc mode, except the double buffer feature of the output compare unit is enabled in the fast pwm mode. tcntn ocrnx update and tovn interrupt flag set 1 period 2 3 ocn ocn (comnx1:0 = 2) (comnx1:0 = 3) ocrnx interrupt flag set 4 5 6 7 f ocnxpwm f clk_i/o n 256 ? ------------------ =
63 attiny13 2535b?avr?01/04 phase correct pwm mode the phase correct pwm mode (wgm02:0 = 1 or 5) provides a high resolution phase correct pwm waveform generation option. the phase correct pwm mode is based on a dual-slope operation. the counter counts repeatedly from bottom to top and then from top to bottom. top is defined as 0xff when wgm2:0 = 1, and ocr0a when wgm2:0 = 5. in non-inverting compare output mode, the output compare (oc0x) is cleared on the compare match between tcnt0 and ocr0x while upcounting, and set on the compare match while down-counting. in inverting output compare mode, the operation is inverted. the dual-slope operation has lower maximum operation frequency than single slope operation. however, due to the symmetric feature of the dual-slope pwm modes, these modes are preferred for motor control applications. in phase correct pwm mode the counter is incremented until the counter value matches top. when the counter reaches top, it changes the count direction. the tcnt0 value will be equal to top for one timer clock cycle. the timing diagram for the phase correct pwm mode is shown on figure 31. the tcnt0 value is in the timing diagram shown as a histogram for illustrating the dual-slope operation. the diagram includes non-inverted and inverted pwm outputs. the small horizontal line marks on the tcnt0 slopes repre- sent compare matches between ocr0x and tcnt0. figure 31. phase correct pwm mode, timing diagram the timer/counter overflow flag (tov0) is set each time the counter reaches bot- tom. the interrupt flag can be used to generate an interrupt each time the counter reaches the bottom value. in phase correct pwm mode, the compare unit allows generation of pwm waveforms on the oc0x pins. setting the com0x1:0 bits to two will produce a non-inverted pwm. an inverted pwm output can be generated by setting the com0x1:0 to three: setting the com0a0 bits to one allows the oc0a pin to toggle on compare matches if the wgm02 bit is set. this option is not available for the oc0b pin (see table 28 on page 67). the actual oc0x value will only be visible on the port pin if the data direction for the port pin is set as output. the pwm waveform is generated by clearing (or setting) the oc0x register at the compare match between ocr0x and tcnt0 when the counter incre- ments, and setting (or clearing) the oc0x register at compare match between ocr0x tovn interrupt flag set ocnx interrupt flag set 1 2 3 tcntn period ocn ocn (comnx1:0 = 2) (comnx1:0 = 3) ocrnx update
64 attiny13 2535b?avr?01/04 and tcnt0 when the counter decrements. the pwm frequency for the output when using phase correct pwm can be calculated by the following equation: the n variable represents the prescale factor (1, 8, 64, 256, or 1024). the extreme values for the ocr0a register represent special cases when generating a pwm waveform output in the phase correct pwm mode. if the ocr0a is set equal to bottom, the output will be continuously low and if set equal to max the output will be continuously high for non-inverted pwm mode. for inverted pwm the output will have the opposite logic values. at the very start of period 2 in figure 31 ocn has a transition from high to low even though there is no compare match. the point of this transition is to guarantee symmetry around bottom. there are two cases that give a transition without compare match.  ocr0a changes its value from max, like in figure 31. when the ocr0a value is max the ocn pin value is the same as the result of a down-counting compare match. to ensure symmetry around bottom the ocn value at max must correspond to the result of an up-counting compare match.  the timer starts counting from a value higher than the one in ocr0a, and for that reason misses the compare match and hence the ocn change that would have happened on the way up. timer/counter timing diagrams the timer/counter is a synchronous design and the timer clock (clk t0 ) is therefore shown as a clock enable signal in the following figures. the figures include information on when interrupt flags are set. figure 32 contains timing data for basic timer/counter operation. the figure shows the count sequence close to the max value in all modes other than phase correct pwm mode. figure 32. timer/counter timing diagram, no prescaling figure 33 shows the same timing data, but with the prescaler enabled. f ocnxpcpwm f clk_i/o n 510 ? ------------------ = clk tn (clk i/o /1) tovn clk i/o tcntn max - 1 max bottom bottom + 1
65 attiny13 2535b?avr?01/04 figure 33. timer/counter timing diagram, with prescaler (f clk_i/o /8) figure 34 shows the setting of ocf0b in all modes and ocf0a in all modes except ctc mode and pwm mode, where ocr0a is top. figure 34. timer/counter timing diagram, setting of ocf0x, with prescaler (f clk_i/o /8) figure 35 shows the setting of ocf0a and the clearing of tcnt0 in ctc mode and fast pwm mode where ocr0a is top. figure 35. timer/counter timing diagram, clear timer on compare match mode, with prescaler (f clk_i/o /8) tovn tcntn max - 1 max bottom bottom + 1 clk i/o clk tn (clk i/o /8) ocfnx ocrnx tcntn ocrnx value ocrnx - 1 ocrnx ocrnx + 1 ocrnx + 2 clk i/o clk tn (clk i/o /8) ocfnx ocrnx tcntn (ctc) top top - 1 top bottom bottom + 1 clk i/o clk tn (clk i/o /8)
66 attiny13 2535b?avr?01/04 8-bit timer/counter register description timer/counter control register a ? tccr0a  bits 7:6 ? com01a:0: compare match output a mode these bits control the output compare pin (oc0a) behavior. if one or both of the com0a1:0 bits are set, the oc0a output overrides the normal port functionality of the i/o pin it is connected to. however, note that the data direction register (ddr) bit cor- responding to the oc0a pin must be set in order to enable the output driver. when oc0a is connected to the pin, the function of the com0a1:0 bits depends on the wgm02:0 bit setting. table 26 shows the com0a1:0 bit functionality when the wgm02:0 bits are set to a normal or ctc mode (non-pwm). table 27 shows the com0a1:0 bit functionality when the wgm01:0 bits are set to fast pwm mode. note: 1. a special case occurs when ocr0a equals top and com0a1 is set. in this case, the compare match is ignored, but the set or clear is done at top. see ?fast pwm mode? on page 61 for more details. bit 76543210 com0a1 com0a0 com0b1 com0b0 ? ? wgm01 wgm00 tccr0a read/write r/w r/w r/w r/w r r r/w r/w initial value 0 0 0 0 0 0 0 0 table 26. compare output mode, non-pwm mode com01 com00 description 0 0 normal port operation, oc0a disconnected. 0 1 toggle oc0a on compare match 1 0 clear oc0a on compare match 1 1 set oc0a on compare match table 27. compare output mode, fast pwm mode (1) com01 com00 description 0 0 normal port operation, oc0a disconnected. 0 1 wgm02 = 0: normal port operation, oc0a disconnected. wgm02 = 1: toggle oc0a on compare match. 1 0 clear oc0a on compare match, set oc0a at top 1 1 set oc0a on compare match, clear oc0a at top
67 attiny13 2535b?avr?01/04 table 28 shows the com0a1:0 bit functionality when the wgm02:0 bits are set to phase correct pwm mode. note: 1. a special case occurs when ocr0a equals top and com0a1 is set. in this case, the compare match is ignored, but the set or clear is done at top. see ?phase cor- rect pwm mode? on page 63 for more details.  bits 5:4 ? com0b1:0: compare match output b mode these bits control the output compare pin (oc0b) behavior. if one or both of the com0b1:0 bits are set, the oc0b output overrides the normal port functionality of the i/o pin it is connected to. however, note that the data direction register (ddr) bit cor- responding to the oc0b pin must be set in order to enable the output driver. when oc0b is connected to the pin, the function of the com0b1:0 bits depends on the wgm02:0 bit setting. table 26 shows the com0a1:0 bit functionality when the wgm02:0 bits are set to a normal or ctc mode (non-pwm). table 27 shows the com0b1:0 bit functionality when the wgm02:0 bits are set to fast pwm mode. note: 1. a special case occurs when ocr0b equals top and com0b1 is set. in this case, the compare match is ignored, but the set or clear is done at top. see ?fast pwm mode? on page 61 for more details. table 28. compare output mode, phase correct pwm mode (1) com0a1 com0a0 description 0 0 normal port operation, oc0a disconnected. 0 1 wgm02 = 0: normal port operation, oc0a disconnected. wgm02 = 1: toggle oc0a on compare match. 1 0 clear oc0a on compare match when up-counting. set oc0a on compare match when down-counting. 1 1 set oc0a on compare match when up-counting. clear oc0a on compare match when down-counting. table 29. compare output mode, non-pwm mode com01 com00 description 0 0 normal port operation, oc0b disconnected. 0 1 toggle oc0b on compare match 1 0 clear oc0b on compare match 1 1 set oc0b on compare match table 30. compare output mode, fast pwm mode (1) com01 com00 description 0 0 normal port operation, oc0b disconnected. 01reserved 1 0 clear oc0b on compare match, set oc0b at top 1 1 set oc0b on compare match, clear oc0b at top
68 attiny13 2535b?avr?01/04 table 28 shows the com0b1:0 bit functionality when the wgm02:0 bits are set to phase correct pwm mode. note: 1. a special case occurs when ocr0b equals top and com0b1 is set. in this case, the compare match is ignored, but the set or clear is done at top. see ?phase cor- rect pwm mode? on page 63 for more details.  bits 3, 2 ? res: reserved bits these bits are reserved bits in the attiny13 and will always read as zero.  bits 1:0 ? wgm01:0: waveform generation mode combined with the wgm02 bit found in the tccr0b register, these bits control the counting sequence of the counter, the source for maximum (top) counter value, and what type of waveform generation to be used, see table 32. modes of operation sup- ported by the timer/counter unit are: normal mode (counter), clear timer on compare match (ctc) mode, and two types of pulse width modulation (pwm) modes (see ?modes of operation? on page 60). notes: 1. max = 0xff 2. bottom = 0x00 table 31. compare output mode, phase correct pwm mode (1) com0a1 com0a0 description 0 0 normal port operation, oc0b disconnected. 01reserved 1 0 clear oc0b on compare match when up-counting. set oc0b on compare match when down-counting. 1 1 set oc0b on compare match when up-counting. clear oc0b on compare match when down-counting. table 32. waveform generation mode bit description mode wgm2 wgm1 wgm0 timer/counter mode of operation top update of ocrx at tov flag set on (1)(2) 0 0 0 0 normal 0xff immediate max 1 0 0 1 pwm, phase correct 0xff top bottom 2 0 1 0 ctc ocra immediate max 3 0 1 1 fast pwm 0xff top max 4 1 0 0 reserved ? ? ? 5 1 0 1 pwm, phase correct ocra top bottom 6 1 1 0 reserved ? ? ? 7 1 1 1 fast pwm ocra top top
69 attiny13 2535b?avr?01/04 timer/counter control register b ? tccr0b  bit 7 ? foc0a: force output compare a the foc0a bit is only active when the wgm bits specify a non-pwm mode. however, for ensuring compatibility with future devices, this bit must be set to zero when tccr0b is written when operating in pwm mode. when writing a logical one to the foc0a bit, an immediate compare match is forced on the waveform generation unit. the oc0a output is changed according to its com0a1:0 bits setting. note that the foc0a bit is implemented as a strobe. therefore it is the value present in the com0a1:0 bits that determines the effect of the forced compare. a foc0a strobe will not generate any interrupt, nor will it clear the timer in ctc mode using ocr0a as top. the foc0a bit is always read as zero.  bit 6 ? foc0b: force output compare b the foc0b bit is only active when the wgm bits specify a non-pwm mode. however, for ensuring compatibility with future devices, this bit must be set to zero when tccr0b is written when operating in pwm mode. when writing a logical one to the foc0b bit, an immediate compare match is forced on the waveform generation unit. the oc0b output is changed according to its com0b1:0 bits setting. note that the foc0b bit is implemented as a strobe. therefore it is the value present in the com0b1:0 bits that determines the effect of the forced compare. a foc0b strobe will not generate any interrupt, nor will it clear the timer in ctc mode using ocr0b as top. the foc0b bit is always read as zero.  bits 5:4 ? res: reserved bits these bits are reserved bits in the attiny13 and will always read as zero.  bit 3 ? wgm02: waveform generation mode see the description in the ?timer/counter control register a ? tccr0a? on page 66.  bits 2:0 ? cs02:0: clock select the three clock select bits select the clock source to be used by the timer/counter. bit 76543210 foc0a foc0b ? ? wgm02 cs02 cs01 cs00 tccr0b read/write w w r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 table 33. clock select bit description cs02 cs01 cs00 description 0 0 0 no clock source (timer/counter stopped) 001clk i/o /(no prescaling) 010clk i/o /8 (from prescaler) 011clk i/o /64 (from prescaler) 100clk i/o /256 (from prescaler)
70 attiny13 2535b?avr?01/04 if external pin modes are used for the timer/counter0, transitions on the t0 pin will clock the counter even if the pin is configured as an output. this feature allows software control of the counting. timer/counter register ? tcnt0 the timer/counter register gives direct access, both for read and write operations, to the timer/counter unit 8-bit counter. writing to the tcnt0 register blocks (removes) the compare match on the following timer clock. modifying the counter (tcnt0) while the counter is running, introduces a risk of missing a compare match between tcnt0 and the ocr0x registers. output compare register a ? ocr0a the output compare register a contains an 8-bit value that is continuously compared with the counter value (tcnt0). a match can be used to generate an output compare interrupt, or to generate a waveform output on the oc0a pin. output compare register b ? ocr0b the output compare register b contains an 8-bit value that is continuously compared with the counter value (tcnt0). a match can be used to generate an output compare interrupt, or to generate a waveform output on the oc0b pin. timer/counter interrupt mask register ? timsk0  bits 7..4, 0 ? res: reserved bits these bits are reserved bits in the attiny13 and will always read as zero.  bit 3 ? ocie0b: timer/counter output compare match b interrupt enable when the ocie0b bit is written to one, and the i-bit in the status register is set, the timer/counter compare match b interrupt is enabled. the corresponding interrupt is 101clk i/o /1024 (from prescaler) 1 1 0 external clock source on t0 pin. clock on falling edge. 1 1 1 external clock source on t0 pin. clock on rising edge. table 33. clock select bit description (continued) cs02 cs01 cs00 description bit 76543210 tcnt0[7:0] tcnt0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 ocr0a[7:0] ocr0a read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 ocr0b[7:0] ocr0b read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543 210 ? ? ? ? ocie0b ocie0a toie0 ? timsk0 read/write r r r r r/w r/w r/w r initial value 0 0 0 0 0 0 0 0
71 attiny13 2535b?avr?01/04 executed if a compare match in timer/counter occurs, i.e., when the ocf0b bit is set in the timer/counter interrupt flag register ? tifr0.  bit 2 ? ocie0a: timer/counter0 output compare match a interrupt enable when the ocie0a bit is written to one, and the i-bit in the status register is set, the timer/counter0 compare match a interrupt is enabled. the corresponding interrupt is executed if a compare match in timer/counter0 occurs, i.e., when the ocf0a bit is set in the timer/counter 0 interrupt flag register ? tifr0.  bit 1 ? toie0: timer/counter0 overflow interrupt enable when the toie0 bit is written to one, and the i-bit in the status register is set, the timer/counter0 overflow interrupt is enabled. the corresponding interrupt is executed if an overflow in timer/counter0 occurs, i.e., when the tov0 bit is set in the timer/counter 0 interrupt flag register ? tifr0. timer/counter 0 interrupt flag register ? tifr0  bits 7..4, 0 ? res: reserved bits these bits are reserved bits in the attiny13 and will always read as zero.  bit 3 ? ocf0b: output compare flag 0 b the ocf0b bit is set when a compare match occurs between the timer/counter and the data in ocr0b ? output compare register0 b. ocf0b is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, ocf0b is cleared by writing a logic one to the flag. when the i-bit in sreg, ocie0b (timer/counter compare b match interrupt enable), and ocf0b are set, the timer/counter compare match interrupt is executed.  bit 2 ? ocf0a: output compare flag 0 a the ocf0a bit is set when a compare match occurs between the timer/counter0 and the data in ocr0a ? output compare register0. ocf0a is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, ocf0a is cleared by writing a logic one to the flag. when the i-bit in sreg, ocie0a (timer/counter0 compare match interrupt enable), and ocf0a are set, the timer/counter0 compare match interrupt is executed.  bit 1 ? tov0: timer/counter0 overflow flag the bit tov0 is set when an overflow occurs in timer/counter0. tov0 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, tov0 is cleared by writing a logic one to the flag. when the sreg i-bit, toie0 (timer/counter0 overflow interrupt enable) , and tov0 are set, the timer/counter0 overflow interrupt is executed. the setting of this flag is dependent of the wgm02:0 bit setting. refer to table 32, ?waveform generation mode bit description? on page 68. bit 76543210 ????ocf0bocf0a tov0 ?tifr0 read/write r r r r r/w r/w r/w r initial value00000000
72 attiny13 2535b?avr?01/04 timer/counter prescaler the timer/counter can be clocked directly by the system clock (by setting the csn2:0 = 1). this provides the fastest operation, with a maximum timer/counter clock frequency equal to system clock frequency (f clk_i/o ). alternatively, one of four taps from the prescaler can be used as a clock source. the prescaled clock has a frequency of either f clk_i/o /8, f clk_i/o /64, f clk_i/o /256, or f clk_i/o /1024. prescaler reset the prescaler is free running, i.e., operates independently of the clock select logic of the timer/counter. since the prescaler is not affected by the timer/counter?s clock select, the state of the prescaler will have implications for situations where a prescaled clock is used. one example of prescaling artifacts occurs when the timer is enabled and clocked by the prescaler (6 > csn2:0 > 1). the number of system clock cycles from when the timer is enabled to the first count occurs can be from 1 to n+1 system clock cycles, where n equals the prescaler divisor (8, 64, 256, or 1024). it is possible to use the prescaler reset for synchronizing the timer/counter to program execution. external clock source an external clock source applied to the t0 pin can be used as timer/counter clock (clk t0 ). the t0 pin is sampled once every system clock cycle by the pin synchronization logic. the synchronized (sampled) signal is then passed through the edge detector. fig- ure 36 shows a functional equivalent block diagram of the t0 synchronization and edge detector logic. the registers are clocked at the positive edge of the internal system clock ( clk i/o ). the latch is transparent in the high period of the internal system clock. the edge detector generates one clk t 0 pulse for each positive (csn2:0 = 7) or negative (csn2:0 = 6) edge it detects. figure 36. t0 pin sampling the synchronization and edge detector logic introduces a delay of 2.5 to 3.5 system clock cycles from an edge has been applied to the t0 pin to the counter is updated. enabling and disabling of the clock input must be done when t0 has been stable for at least one system clock cycle, otherwise it is a risk that a false timer/counter clock pulse is generated. each half period of the external clock applied must be longer than one system clock cycle to ensure correct sampling. the exter nal clock must be guaranteed to have less than half the system clock frequency (f extclk < f clk_i/o /2) given a 50/50% duty cycle. since the edge detector uses sampling, the maximum frequency of an external clock it can detect is half the sampling frequency (nyquist sampling theorem). however, due to vari- ation of the system clock frequency and duty cycle caused by oscillator source (crystal, resonator, and capacitors) tolerances, it is recommended that maximum frequency of an external clock source is less than f clk_i/o /2.5. an external clock source can not be prescaled. tn_sync (to clock select logic) edge detector synchronization dq dq le dq tn clk i/o
73 attiny13 2535b?avr?01/04 figure 37. prescaler for timer/counter0 note: 1. the synchronization logic on the input pins ( t0) is shown in figure 36. general timer/counter control register ? gtccr  bit 7 ? tsm: timer/counter synchronization mode writing the tsm bit to one activates the timer/counter synchronization mode. in this mode, the value that is written to the psr10 bit is kept, hence keeping the prescaler reset signal asserted. this ensures that the timer/counter is halted and can be config- ured without the risk of advancing during configuration. when the tsm bit is written to zero, the psr10 bit is cleared by hardware, and the timer/counter start counting.  bit 0 ? psr10: prescaler reset timer/counter0 when this bit is one, the timer/counter0 prescaler will be reset. this bit is normally cleared immediately by hardware, except if the tsm bit is set. psr10 clear clk t0 t0 clk i/o synchronization bit 7 6 5 4 3 2 1 0 tsm ? ? ? ? ? ? psr10 gtccr read/write r/w r r r r r r r/w initial value 0 0 0 0 0 0 0 0
74 attiny13 2535b?avr?01/04 analog comparator the analog comparator compares the input values on the positive pin ain0 and nega- tive pin ain1. when the voltage on the positive pin ain0 is higher than the voltage on the negative pin ain1, the analog comparator output, aco, is set. the comparator can trigger a separate interrupt, exclusive to the analog comparator. the user can select interrupt triggering on comparator output rise, fall or toggle. a block diagram of the com- parator and its surrounding logic is shown in figure 38 . figure 38. analog comparator block diagram (2) notes: 1. see table 35 on page 76. 2. refer to figure 1 on page 1 and table 23 on page 50 for analog comparator pin placement. adc control and status register b ? adcsrb  bit 6 ? acme: analog comparator multiplexer enable when this bit is written logic one and the adc is switched off (aden in adcsra is zero), the adc multiplexer selects the negative input to the analog comparator. when this bit is written logic zero, ain1 is applied to the negative input of the analog compar- ator. for a detailed description of this bit, see ?analog comparator multiplexed input? on page 76. analog comparator control and status register ? acsr  bit 7 ? acd: analog comparator disable when this bit is written logic one, the power to the analog comparator is switched off. this bit can be set at any time to turn off the analog comparator. this will reduce power consumption in active and idle mode. when changing the acd bit, the analog compar- ator interrupt must be disabled by clearing the acie bit in acsr. otherwise an interrupt can occur when the bit is changed.  bit 6 ? acbg: analog comparator bandgap select acbg bandgap reference adc multiplexer output acme aden (1) bit 7 6543210 ? acme ? ? ? adts2 adts1 adts0 adcsrb read/write r r/w r r r r/w r/w r/w initial value0 0000000 bit 76543210 acd acbg aco aci acie ? acis1 acis0 acsr read/write r/w r/w r r/w r/w r r/w r/w initial value 0 0 n/a 0 0 0 0 0
75 attiny13 2535b?avr?01/04 when this bit is set, a fixed bandgap reference voltage replaces the positive input to the analog comparator. when this bit is cleared, ain0 is applied to the positive input of the analog comparator.  bit 5 ? aco: analog comparator output the output of the analog comparator is synchronized and then directly connected to aco. the synchronization introduces a delay of 1 - 2 clock cycles.  bit 4 ? aci: analog comparator interrupt flag this bit is set by hardware when a comparator output event triggers the interrupt mode defined by acis1 and acis0. the analog comparator interrupt routine is executed if the acie bit is set and the i-bit in sreg is set. aci is cleared by hardware when execut- ing the corresponding interrupt handling vector. alternatively, aci is cleared by writing a logic one to the flag.  bit 3 ? acie: analog comparator interrupt enable when the acie bit is written logic one and the i-bit in the status register is set, the ana- log comparator interrupt is activated. when written logic zero, the interrupt is disabled.  bit 2 ? res: reserved bit this bit is a reserved bit in the attiny13 and will always read as zero.  bits 1, 0 ? acis1, acis0: analog comparator interrupt mode select these bits determine which comparator events that trigger the analog comparator inter- rupt. the different settings are shown in table 34. when changing the acis1/acis0 bits, the analog comparator interrupt must be dis- abled by clearing its interrupt enable bit in the acsr register. otherwise an interrupt can occur when the bits are changed. table 34. acis1/acis0 settings acis1 acis0 interrupt mode 0 0 comparator interrupt on output toggle. 01reserved 1 0 comparator interrupt on falling output edge. 1 1 comparator interrupt on rising output edge.
76 attiny13 2535b?avr?01/04 analog comparator multiplexed input it is possible to select any of the adc3..0 pins to replace the negative input to the ana- log comparator. the adc multiplexer is used to select this input, and consequently, the adc must be switched off to utilize this feature. if the analog comparator multiplexer enable bit (acme in adcsrb) is set and the adc is switched off (aden in adcsra is zero), mux1..0 in admux select the input pin to replace the negative input to the ana- log comparator, as shown in table 35. if acme is cleared or aden is set, ain1 is applied to the negative input to the analog comparator. digital input disable register 0 ? didr0  bits 1, 0 ? ain1d, ain0d: ain1, ain0 digital input disable when this bit is written logic one, the digital input buffer on the ain1/0 pin is disabled. the corresponding pin register bit will always read as zero when this bit is set. when an analog signal is applied to the ain1/0 pin and the digital input from this pin is not needed, this bit should be written logic one to reduce power consumption in the digital input buffer. table 35. analog comparator multiplexed input acme aden mux1..0 analog comparator negative input 0x xxain1 11 xxain1 1 0 00 adc0 1 0 01 adc1 1 0 10 adc2 1 0 11 adc3 bit 76543210 ? ? adc0d adc2d adc3d adc1d ain1d ain0d didr0 read/write r r r/w r/w r/w r/w r/w r/w initial value00000000
77 attiny13 2535b?avr?01/04 analog to digital converter features  10-bit resolution  0.5 lsb integral non-linearity  2 lsb absolute accuracy  13 - 260 s conversion time  up to 15 ksps at maximum resolution  four multiplexed single ended input channels  optional left adjustment for adc result readout  0 - v cc adc input voltage range  selectable 1.1v adc reference voltage  free running or single conversion mode  adc start conversion by auto triggering on interrupt sources  interrupt on adc conversion complete  sleep mode noise canceler the attiny13 features a 10-bit successive approximation adc. the adc is connected to a 4-channel analog multiplexer which allows four single-ended voltage inputs con- structed from the pins of port b. the single-ended voltage inputs refer to 0v (gnd). the adc contains a sample and hold circuit which ensures that the input voltage to the adc is held at a constant level during conversion. a block diagram of the adc is shown in figure 39. internal reference voltages of nominally 1.1v or v cc are provided on-chip.
78 attiny13 2535b?avr?01/04 figure 39. analog to digital converter block schematic operation the adc converts an analog input voltage to a 10-bit digital value through successive approximation. the minimum value represents gnd and the maximum value represents the voltage on v cc or an internal 1.1v reference voltage. the analog input channel is selected by writing to the mux bits in admux. any of the adc input pins, can be selected as single ended inputs to the adc. the adc is enabled by setting the adc enable bit, aden in adcsra. voltage refer- ence and input channel selections w ill not go into effect until aden is set. the adc does not consume power when aden is cleared, so it is recommended to switch off the adc before entering power saving sleep modes. the adc generates a 10-bit result which is presented in the adc data registers, adch and adcl. by default, the result is presented right adjusted, but can optionally be presented left adjusted by setting the adlar bit in admux. if the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read adch. otherwise, adcl must be read first, then adch, to ensure that the content of the data registers belongs to the same conversion. once adcl is read, adc access to data registers is blocked. this means that if adcl has been read, and a conversion completes before adch is read, neither register is updated and the result from the con- adc conversion complete irq 8-bit data bus 15 0 adc multiplexer select (admux) adc ctrl. & status register (adcsra) adc data register (adch/adcl) adie adate adsc aden adif adif mux1 mux0 adps0 adps1 adps2 conversion logic 10-bit dac + - sample & hold comparator internal 1.1v reference mux decoder v cc adc3 adc2 adc1 adc0 refs1 adlar channel selection adc[9:0] adc multiplexer output prescaler input mux trigger select adts[2:0] interrupt flags start
79 attiny13 2535b?avr?01/04 version is lost. when adch is read, adc access to the adch and adcl registers is re-enabled. the adc has its own interrupt which can be triggered when a conversion completes. when adc access to the data registers is prohibited between reading of adch and adcl, the interrupt will trigger even if the result is lost. starting a conversion a single conversion is started by writing a logical one to the adc start conversion bit, adsc. this bit stays high as long as the conversion is in progress and will be cleared by hardware when the conversion is completed. if a different data channel is selected while a conversion is in progress, the adc will finish the current conversion before performing the channel change. alternatively, a conversion can be triggered automatically by various sources. auto trig- gering is enabled by setting the adc auto trigger enable bit, adate in adcsra. the trigger source is selected by setting the adc trigger select bits, adts in adcsrb (see description of the adts bits for a list of the trigger sources). when a positive edge occurs on the selected trigger signal, the adc prescaler is reset and a conversion is started. this provides a method of starting conversions at fixed intervals. if the trigger signal still is set when the conversion completes, a new conversion will not be started. if another positive edge occurs on the trigger signal during conversion, the edge will be ignored. note that an interrupt flag will be set even if the specific interrupt is disabled or the global interrupt enable bit in sreg is cleared. a conversion can thus be triggered without causing an interrupt. however, the interrupt flag must be cleared in order to trig- ger a new conversion at the next interrupt event. figure 40. adc auto trigger logic using the adc interrupt flag as a trigger source makes the adc start a new conversion as soon as the ongoing conversion has fini shed. the adc then operates in free run- ning mode, constantly sampling and updating the adc data register. the first conversion must be started by writing a logical one to the adsc bit in adcsra. in this mode the adc will perform successive conversions independently of whether the adc interrupt flag, adif is cleared or not. if auto triggering is enabled, single conversions can be started by writing adsc in adcsra to one. adsc can also be used to determine if a conversion is in progress. the adsc bit will be r ead as one during a conversion, independently of how the conver- sion was started. adsc adif source 1 source n adts[2:0] conversion logic prescaler start clk adc . . . . edge detector adate
80 attiny13 2535b?avr?01/04 prescaling and conversion timing figure 41. adc prescaler by default, the successive approximation circuitry requires an input clock frequency between 50 khz and 200 khz to get maximum resolution. if a lower resolution than 10 bits is needed, the input clock frequency to the adc can be higher than 200 khz to get a higher sample rate. the adc module contains a prescaler, which generates an acceptable adc clock fre- quency from any cpu frequency above 100 khz. the prescaling is set by the adps bits in adcsra. the prescaler starts counting from the moment the adc is switched on by setting the aden bit in adcsra. the prescaler keeps running for as long as the aden bit is set, and is continuously reset when aden is low. when initiating a single ended conversion by setting the adsc bit in adcsra, the con- version starts at the following rising edge of the adc clock cycle. a normal conversion takes 13 adc clock cycles. the first conversion after the adc is switched on (aden in adcsra is set) takes 25 adc clock cycles in order to initialize the analog circuitry. the actual sample-and-hold takes place 1.5 adc clock cycles after the start of a normal conversion and 14.5 adc clock cycles after the start of an first conversion. when a con- version is complete, the result is written to the adc data registers, and adif is set. in single conversion mode, adsc is cleared simultaneously. the software may then set adsc again, and a new conversion will be initiated on the first rising adc clock edge. when auto triggering is used, the prescaler is reset when the trigger event occurs. this assures a fixed delay from the trigger event to the start of conversion. in this mode, the sample-and-hold takes place two adc clock cycles after the rising edge on the trigger source signal. three additional cpu clock cycles are used for synchronization logic. in free running mode, a new conversion will be started immediately after the conver- sion completes, while adsc remains high. for a summary of conversion times, see table 36. 7-bit adc prescaler adc clock source ck adps0 adps1 adps2 ck/128 ck/2 ck/4 ck/8 ck/16 ck/32 ck/64 reset aden start
81 attiny13 2535b?avr?01/04 figure 42. adc timing diagram, first conversion (single conversion mode) figure 43. adc timing diagram, single conversion figure 44. adc timing diagram, auto triggered conversion sign and msb of result lsb of result adc clock adsc sample & hold adif adch adcl cycle number aden 1 212 13 14 15 16 17 18 19 20 21 22 23 24 25 1 2 first conversion next conversion 3 mux and refs update mux and refs update conversion complete 1 2 3 4 5 6 7 8 9 10 11 12 13 sign and msb of result lsb of result adc clock adsc adif adch adcl cycle number 12 one conversion next conversion 3 sample & hold mux and refs update conversion complete mux and refs update 1 2 3 4 5 6 7 8 9 10 11 12 13 sign and msb of result lsb of result adc clock trigger source adif adch adcl cycle number 12 one conversion next conversion conversion complete prescaler reset adate prescaler reset sample & hold mux and refs update
82 attiny13 2535b?avr?01/04 figure 45. adc timing diagram, free running conversion table 36. adc conversion time condition sample & hold (cycles from start of conversion) conversion time (cycles) first conversion 14.5 25 normal conversions 1.5 13 auto triggered conversions 2 13.5 11 12 13 sign and msb of result lsb of result adc clock adsc adif adch adcl cycle number 12 one conversion next conversion 34 conversion complete sample & hold mux and refs update
83 attiny13 2535b?avr?01/04 changing channel or reference selection the muxn and refs1:0 bits in the admux register are single buffered through a tem- porary register to which the cpu has random access. this ensures that the channels and reference selection only takes place at a safe point during the conversion. the channel and reference selection is continuously updated until a conversion is started. once the conversion starts, the channel and reference selection is locked to ensure a sufficient sampling time for the adc. continuous updating resumes in the last adc clock cycle before the conversion completes (adif in adcsra is set). note that the conversion starts on the following rising adc clock edge after adsc is written. the user is thus advised not to write new channel or reference selection values to admux until one adc clock cycle after adsc is written. if auto triggering is used, the exact time of the triggering event can be indeterministic. special care must be taken when updating the admux register, in order to control which conversion will be affected by the new settings. if both adate and aden is written to one, an interrupt event can occur at any time. if the admux register is changed in this period, the user cannot tell if the next conversion is based on the old or the new settings. admux can be safely updated in the following ways: 1. when adate or aden is cleared. 2. during conversion, minimum one adc clock cycle after the trigger event. 3. after a conversion, before the interrupt flag used as trigger source is cleared. when updating admux in one of these conditions, the new settings will affect the next adc conversion. adc input channels when changing channel selections, the user should observe the following guidelines to ensure that the correct channel is selected: in single conversion mode, always select the channel before starting the conversion. the channel selection may be changed one adc clock cycle after writing one to adsc. however, the simplest method is to wait for the conversion to complete before changing the channel selection. in free running mode, always select the channel before starting the first conversion. the channel selection may be changed one adc clock cycle after writing one to adsc. however, the simplest method is to wait for the first conversion to complete, and then change the channel selection. since the next conversion has already started automati- cally, the next result will reflect the previous channel selection. subsequent conversions will reflect the new channel selection.
84 attiny13 2535b?avr?01/04 adc voltage reference the reference voltage for the adc (v ref ) indicates the conversion range for the adc. single ended channels that exceed v ref will result in codes close to 0x3ff. v ref can be selected as either v cc , or internal 1.1v reference, or external aref pin. the first adc conversion result after switching reference voltage source may be inaccurate, and the user is advised to discard this result. adc noise canceler the adc features a noise canceler that enables conversion during sleep mode to reduce noise induced from the cpu core and other i/o peripherals. the noise canceler can be used with adc noise reduction and idle mode. to make use of this feature, the following procedure should be used: 1. make sure that the adc is enabled and is not busy converting. single con- version mode must be selected and the adc conversion complete interrupt must be enabled. 2. enter adc noise reduction mode (or idle mode). the adc will start a con- version once the cpu has been halted. 3. if no other interrupts occur before the adc conversion completes, the adc interrupt will wake up the cpu and execute the adc conversion complete interrupt routine. if another interrupt wakes up the cpu before the adc con- version is complete, that interrupt will be executed, and an adc conversion complete interrupt request will be generated when the adc conversion completes. the cpu will remain in active mode until a new sleep command is executed. note that the adc will not be automatically turned off when entering other sleep modes than idle mode and adc noise reduction mode. the user is advised to write zero to aden before entering such sleep modes to avoid excessive power consumption.
85 attiny13 2535b?avr?01/04 analog input circuitry the analog input circuitry for single ended channels is illustrated in figure 46. an analog source applied to adcn is subjected to the pin capacitance and input leakage of that pin, regardless of whether that channel is selected as input for the adc. when the chan- nel is selected, the source must drive the s/h capacitor through the series resistance (combined resistance in the input path). the adc is optimized for analog signals with an output impedance of approximately 10 k ? or less. if such a source is used, the sampling time will be negligible. if a source with higher impedance is used, the sampling time will depend on how long time the source needs to charge the s/h capacitor, with can vary widely. the user is recom- mended to only use low impedant sources with slowly varying signals, since this minimizes the required charge transfer to the s/h capacitor. signal components higher than the nyquist frequency (f adc /2) should not be present to avoid distortion from unpredictable signal convolution. the user is advised to remove high frequency components with a low-pass filter before applying the signals as inputs to the adc. figure 46. analog input circuitry adcn i ih 1..100 k ? c s/h = 14 pf v cc /2 i il
86 attiny13 2535b?avr?01/04 analog noise canceling techniques digital circuitry inside and outside the device generates emi which might affect the accuracy of analog measurements. if conversion accuracy is critical, the noise level can be reduced by applying the following techniques: 1. keep analog signal paths as short as possible. make sure analog tracks run over the analog ground plane, and keep them well away from high-speed switching digital tracks. 2. use the adc noise canceler function to reduce induced noise from the cpu. 3. if any port pins are used as digital outputs, it is essential that these do not switch while a conversion is in progress. adc accuracy definitions an n-bit single-ended adc converts a voltage linearly between gnd and v ref in 2 n steps (lsbs). the lowest code is read as 0, and the highest code is read as 2 n -1. several parameters describe the deviation from the ideal behavior:  offset: the deviation of the first transition (0x000 to 0x001) compared to the ideal transition (at 0.5 lsb). ideal value: 0 lsb. figure 47. offset error  gain error: after adjusting for offset, the gain error is found as the deviation of the last transition (0x3fe to 0x3ff) compared to the ideal transition (at 1.5 lsb below maximum). ideal value: 0 lsb output code v ref input voltage ideal adc actual adc offset error
87 attiny13 2535b?avr?01/04 figure 48. gain error  integral non-linearity (inl): after adjusting for offset and gain error, the inl is the maximum deviation of an actual transition compared to an ideal transition for any code. ideal value: 0 lsb. figure 49. integral non-linearity (inl)  differential non-linearity (dnl): the maximum deviation of the actual code width (the interval between two adjacent transitions) from the ideal code width (1 lsb). ideal value: 0 lsb. output code v ref input voltage ideal adc actual adc gain error output code v ref input voltage ideal adc actual adc inl
88 attiny13 2535b?avr?01/04 figure 50. differential non-linearity (dnl)  quantization error: due to the quantization of the input voltage into a finite number of codes, a range of input voltages (1 lsb wide) will code to the same value. always 0.5 lsb.  absolute accuracy: the maximum deviation of an actual (unadjusted) transition compared to an ideal transition for any code. this is the compound effect of offset, gain error, differential error, non-linearity, and quantization error. ideal value: 0.5 lsb. adc conversion result after the conversion is complete (adif is high), the conversion result can be found in the adc result registers (adcl, adch). for single ended conversion, the result is where v in is the voltage on the selected input pin and v ref the selected voltage refer- ence (see table 37 on page 89 and table 38 on page 89). 0x000 represents analog ground, and 0x3ff represents the selected reference voltage minus one lsb. adc multiplexer selection register ? admux  bit 7 ? res: reserved bit this bit is reserved bit in the attiny13 and will always read as zero.  bit 6 ? refs0: reference selection bit output code 0x3ff 0x000 0 v ref input voltage dnl 1 lsb adc v in 1024 ? v ref -------------------------- = bit 76543210 ? refs0 adlar ? ? ? mux1 mux0 admux read/write r r/w r/w r r r r/w r/w initial value00000000
89 attiny13 2535b?avr?01/04 this bit selects the voltage reference for the adc, as shown in table 37. if this bit is changed during a conversion, the change will not go in effect until this conversion is complete (adif in adcsra is set).  bit 5 ? adlar: adc left adjust result the adlar bit affects the presentation of the adc conversion result in the adc data register. write one to adlar to left adjust the result. otherwise, the result is right adjusted. changing the adlar bit will affect the adc data register immediately, regardless of any ongoing conversions. for a complete description of this bit, see ?the adc data register ? adcl and adch? on page 90.  bits 4:2 ? res: reserved bits these bits are reserved bits in the attiny13 and will always read as zero.  bits 1:0 ? mux1:0: analog channel selection bits the value of these bits selects which combination of analog inputs are connected to the adc. see table 38 for details. if these bits are changed during a conversion, the change will not go in effect until this conversion is complete (adif in adcsra is set). adc control and status register a ? adcsra  bit 7 ? aden: adc enable writing this bit to one enables the adc. by writing it to zero, the adc is turned off. turn- ing the adc off while a conversion is in progress, will terminate this conversion.  bit 6 ? adsc: adc start conversion in single conversion mode, write this bit to one to start each conversion. in free run- ning mode, write this bit to one to start the first conversion. the first conversion after adsc has been written after the adc has been enabled, or if adsc is written at the same time as the adc is enabled, will ta ke 25 adc clock cycles instead of the normal 13. this first conversion performs initialization of the adc. adsc will read as one as long as a conversi on is in progress. when the conversion is complete, it returns to zero. writing zero to this bit has no effect.  bit 5 ? adate: adc auto trigger enable table 37. voltage reference selections for adc refs0 voltage reference selection 0v cc used as analog reference. 1 internal voltage reference. table 38. input channel selections mux1..0 single ended input 00 adc0 (pb5) 01 adc1 (pb2) 10 adc2 (pb4) 11 adc3 (pb3) bit 76543210 aden adsc adate adif adie adps2 adps1 adps0 adcsra read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
90 attiny13 2535b?avr?01/04 when this bit is written to one, auto triggering of the adc is enabled. the adc will start a conversion on a positive edge of the selected trigger signal. the trigger source is selected by setting the adc trigger select bits, adts in adcsrb.  bit 4 ? adif: adc interrupt flag this bit is set when an adc conversion completes and the data registers are updated. the adc conversion complete interrupt is executed if the adie bit and the i-bit in sreg are set. adif is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, adif is cleared by writing a logical one to the flag. beware that if doing a read-modify-write on adcsra, a pending interrupt can be dis- abled. this also applies if the sbi and cbi instructions are used.  bit 3 ? adie: adc interrupt enable when this bit is written to one and the i-bit in sreg is set, the adc conversion com- plete interrupt is activated.  bits 2:0 ? adps2:0: adc prescaler select bits these bits determine the division factor between the system clock frequency and the input clock to the adc. the adc data register ? adcl and adch adlar = 0 adlar = 1 table 39. adc prescaler selections adps2 adps1 adps0 division factor 000 2 001 2 010 4 011 8 100 16 101 32 110 64 1 1 1 128 bit 151413121110 9 8 ?????? adc9 adc8 adch adc7 adc6 adc5 adc4 adc3 adc2 adc1 adc0 adcl 76543210 read/writerrrrrrrr rrrrrrrr initial value00000000 00000000 bit 151413121110 9 8 adc9 adc8 adc7 adc6 adc5 adc4 adc3 adc2 adch adc1 adc0 ?????? adcl 76543210 read/writerrrrrrrr rrrrrrrr
91 attiny13 2535b?avr?01/04 when an adc conversion is complete, the result is found in these two registers. when adcl is read, the adc data register is not updated until adch is read. conse- quently, if the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read adch. otherwise, adcl must be read first, then adch. the adlar bit in admux, and the muxn bits in admux affect the way the result is read from the registers. if adlar is set, the result is left adjusted. if adlar is cleared (default), the result is right adjusted.  adc9:0: adc conversion result these bits represent the result from the conversion, as detailed in ?adc conversion result? on page 88. adc control and status register b ? adcsrb  bits 7, 5..3 ? res: reserved bits these bits are reserved bits in the attiny13 and will always read as zero.  bits 2:0 ? adts2:0: adc auto trigger source if adate in adcsra is written to one, the value of these bits selects which source will trigger an adc conversion. if adate is cleared, the adts2:0 settings will have no effect. a conversion will be triggered by the rising edge of the selected interrupt flag. note that switching from a trigger source that is cleared to a trigger source that is set, will generate a positive edge on the trigger signal. if aden in adcsra is set, this will start a conversion. switching to free running mode (adts[2:0]=0) will not cause a trig- ger event, even if the adc interrupt flag is set . digital input disable register 0 ? didr0  bits 5..2 ? adc3d..adc0d: adc3..0 digital input disable initial value00000000 00000000 bit 76543210 ? acme ? ? ? adts2 adts1 adts0 adcsrb read/write r r/w r r r r/w r/w r/w initial value 0 0 0 0 0 0 0 0 table 40. adc auto trigger source selections adts2 adts1 adts0 trigger source 0 0 0 free running mode 0 0 1 analog comparator 0 1 0 external interrupt request 0 0 1 1 timer/counter compare match a 1 0 0 timer/counter overflow 1 0 1 timer/counter compare match b 1 1 0 pin change interrupt request bit 76543210 ? ? adc0d adc2d adc3d adc1d ain1d ain0d didr0 read/write r r r/w r/w r/w r/w r/w r/w initial value00000000
92 attiny13 2535b?avr?01/04 when this bit is written logic one, the digital input buffer on the corresponding adc pin is disabled. the corresponding pin register bit will always read as zero when this bit is set. when an analog signal is applied to the adc3..0 pin and the digital input from this pin is not needed, this bit should be written logic one to reduce power consumption in the dig- ital input buffer.
93 attiny13 2535b?avr?01/04 debugwire on-chip debug system features  complete program flow control  emulates all on-chip functions, both digital and analog, except reset pin  real-time operation  symbolic debugging support (both at c and assembler source level, or for other hlls)  unlimited number of program break points (using software break points)  non-intrusive operation  electrical characteristics identical to real device  automatic configuration system  high-speed operation  programming of non-volatile memories overview the debugwire on-chip debug system uses a one-wire, bi-directional interface to con- trol the program flow, execute avr instructions in the cpu and to program the different non-volatile memories. physical interface when the debugwire enable (dwen) fuse is programmed and lock bits are unpro- grammed, the debugwire system within the target device is activated. the reset port pin is configured as a wire-and (open-drain) bi-directional i/o pin with pull-up enabled and becomes the communication gateway between target and emulator. figure 51. the debugwire setup figure 51 shows the schematic of a target mcu, with debugwire enabled, and the emulator connector. the system clock is not affected by debugwire and will always be the clock source selected by the cksel fuses. when designing a system where debugwire will be used, the following observations must be made for correct operation:  pull-up resistor on the dw/(reset) line must be in the range of 10k to 20 k ? . however, the pull-up resistor is optional.  connecting the reset pin directly to v cc will not work. dw gnd dw(reset) vcc 1.8 - 5.5v
94 attiny13 2535b?avr?01/04  capacitors inserted on the reset pin must be disconnected when using debugwire.  all external reset sources must be disconnected. software break points debugwire supports program memory break points by the avr break instruction. set- ting a break point in avr studio ? will insert a break instruction in the program memory. the instruction replaced by the break instruction will be stored. when pro- gram execution is continued, the stored instruction will be executed before continuing from the program memory. a break can be inserted manually by putting the break instruction in the program. the flash must be re-programmed each time a break point is changed. this is auto- matically handled by avr studio through the debugwire interface. the use of break points will therefore reduce the flash data retention. devices used for debugging pur- poses should not be shipped to end customers. limitations of debugwire the debugwire communication pin (dw) is physically located on the same pin as external reset (reset). an external reset source is therefore not supported when the debugwire is enabled. the debugwire system accurately emulates all i/o functions when running at full speed, i.e., when the program in the cpu is running. when the cpu is stopped, care must be taken while accessing some of the i/o registers via the debugger (avr stu- dio). see the debugwire documentation for detailed description of the limitations. a programmed dwen fuse enables some parts of the clock system to be running in all sleep modes. this will increase the power consumption while in sleep. thus, the dwen fuse should be disabled when debugwire is not used. debugwire related register in i/o memory the following section describes the registers used with the debugwire. debugwire data register ? dwdr the dwdr register provides a communication channel from the running program in the mcu to the debugger. this register is only accessible by the debugwire and can therefore not be used as a general purpose register in the normal operations. bit 76543210 dwdr[7:0] dwdr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
95 attiny13 2535b?avr?01/04 self-programming the flash the device provides a self-programming mechanism for downloading and uploading program code by the mcu itself. the self -programming can use any available data interface and associated protocol to read code and write (program) that code into the program memory. the program memory is updated in a page by page fashion. before programming a page with the data stored in the temporary page buffer, the page must be erased. the temporary page buffer is filled one word at a time using spm and the buffer can be filled either before the page erase command or between a page erase and a page write operation: alternative 1, fill the buffer before a page erase  fill temporary page buffer  perform a page erase  perform a page write alternative 2, fill the buffer after page erase  perform a page erase  fill temporary page buffer  perform a page write if only a part of the page needs to be changed, the rest of the page must be stored (for example in the temporary page buffer) before the erase, and then be re-written. when using alternative 1, the boot loader provides an effective read-modify-write feature which allows the user software to first read the page, do the necessary changes, and then write back the modified data. if alternative 2 is used, it is not possible to read the old data while loading since the page is already erased. the temporary page buffer can be accessed in a random sequence. it is essential that the page address used in both the page erase and page write operation is addressing the same page. performing page erase by spm to execute page erase, set up the address in the z-pointer, write ?00000011? to spmcsr and execute spm within four clock cycles after writing spmcsr. the data in r1 and r0 is ignored. the page address must be written to pcpage in the z-register. other bits in the z-pointer will be ignored during this operation.  the cpu is halted during the page erase operation. filling the temporary buffer (page loading) to write an instruction word, set up the address in the z-pointer and data in r1:r0, write ?00000001? to spmcsr and execute spm within four clock cycles after writing spmcsr. the content of pcword in the z-register is used to address the data in the temporary buffer. the temporary buffer will auto-erase after a page write operation or by writing the ctpb bit in spmcsr. it is also erased after a system reset. note that it is not possible to write more than one time to each address without erasing the temporary buffer. if the eeprom is written in the middle of an spm page load operation, all data loaded will be lost. performing a page write to execute page write, set up the addre ss in the z-pointer, write ?00000101? to spmcsr and execute spm within four clock cycles after writing spmcsr. the data in r1 and r0 is ignored. the page address must be written to pcpage. other bits in the z-pointer must be written to zero during this operation.  the cpu is halted during the page write operation.
96 attiny13 2535b?avr?01/04 addressing the flash during self- programming the z-pointer is used to address the spm commands. since the flash is organized in pages (see table 46 on page 102), the program counter can be treated as having two different sections. one section, consisting of the least sig- nificant bits, is addressing the words within a page, while the most significant bits are addressing the pages. this is shown in figure 52. note that the page erase and page write operations are addressed independently. ther efore it is of major importance that the software addresses the same page in both the page erase and page write operation. the lpm instruction uses the z-pointer to store the address. since this instruction addresses the flash byte-by-byte, also the lsb (bit z0) of the z-pointer is used. figure 52. addressing the flash during spm (1) note: 1. the different variables used in figure 52 are listed in table 46 on page 102. bit 151413121110 9 8 zh (r31) z15 z14 z13 z12 z11 z10 z9 z8 zl (r30) z7z6z5z4z3z2z1z0 76543210 program memory 0 1 15 z - register bit 0 zpagemsb word address within a page page address within the flash zpcmsb instruction word pag e pcword[pagemsb:0]: 00 01 02 pageend pag e pcword pcpage pcmsb pagemsb program counter
97 attiny13 2535b?avr?01/04 store program memory control and status register ? spmcsr the store program memory control and status register contains the control bits needed to control the program memory operations.  bits 7..5 ? res: reserved bits these bits are reserved bits in the attiny13 and always read as zero.  bit 4 ? ctpb: clear temporary page buffer if the ctpb bit is written while filling the temporary page buffer, the temporary page buffer will be cleared and the data will be lost.  bit 3 ? rflb: read fuse and lock bits an lpm instruction within three cycles after rflb and selfprgen are set in the spmcsr register, will read either the lock bits or the fuse bits (depending on z0 in the z-pointer) into the destination register. see ?eeprom write prevents writing to spmcsr? on page 98 for details.  bit 2 ? pgwrt: page write if this bit is written to one at the same time as selfprgen, the next spm instruction within four clock cycles executes page writ e, with the data stor ed in the temporary buffer. the page address is taken from the high part of the z-pointer. the data in r1 and r0 are ignored. the pgwrt bit will auto-clear upon completion of a page write, or if no spm instruction is executed within four clock cycles. the cpu is halted during the entire page write operation.  bit 1 ? pgers: page erase if this bit is written to one at the same time as selfprgen, the next spm instruction within four clock cycles executes page erase. the page address is taken from the high part of the z-pointer. the data in r1 and r0 are ignored. the pgers bit will auto-clear upon completion of a page erase, or if no spm instruction is executed within four clock cycles. the cpu is halted during the entire page write operation.  bit 0 ? selfprgen: self programming enable this bit enables the spm instruction for the next four clock cycles. if written to one together with either ctpb, rflb, pgwrt, or pgers, the following spm instruction will have a special meaning, see description above. if only selfprgen is written, the following spm instruction will store the value in r1:r0 in the temporary page buffer addressed by the z-pointer. the lsb of the z-pointer is ignored. the selfprgen bit will auto-clear upon completion of an spm instruction, or if no spm instruction is exe- cuted within four clock cycles. during page erase and page write, the selfprgen bit remains high until the operation is completed. writing any other combination than ?10001?, ?01001?, ?00101?, ?00011? or ?00001? in the lower five bits will have no effect. bit 7654321 0 ? ? ? ctpb rflb pgwrt pgers selfprgen spmcsr read/write r r r r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
98 attiny13 2535b?avr?01/04 eeprom write prevents writing to spmcsr note that an eeprom write operation will block all software programming to flash. reading the fuses and lock bits from software will also be prevented during the eeprom write operation. it is recommended that the user checks the status bit (eewe) in the eecr register and verifies that the bit is cleared before writing to the spmcsr register. reading the fuse and lock bits from software it is possible to read both the fuse and lock bits from software. to read the lock bits, load the z-pointer with 0x0001 and set the rflb and selfprgen bits in spmcsr. when an lpm instruction is executed within three cpu cycles after the rflb and selfprgen bits are set in spmcsr, the value of the lock bits will be loaded in the destination register. the rflb and selfprgen bits will auto-clear upon completion of reading the lock bits or if no lpm instruction is executed within three cpu cycles or no spm instruction is executed within four cpu cycles. when rflb and selfprgen are cleared, lpm will work as described in the instruction set manual. the algorithm for reading the fuse low byte is similar to the one described above for reading the lock bits. to read the fuse low byte, load the z-pointer with 0x0000 and set the rflb and selfprgen bits in spmcsr. when an lpm instruction is executed within three cycles after the rflb and selfprgen bits are set in the spmcsr, the value of the fuse low byte (flb) will be loaded in the destination register as shown below. refer to table 45 on page 101 for a detailed description and mapping of the fuse low byte. similarly, when reading the fuse high byte, load 0x0003 in the z-pointer. when an lpm instruction is executed within three cycles after the rflb and selfprgen bits are set in the spmcsr, the value of the fuse high byte (fhb) will be loaded in the destination register as shown below. refer to table xxx on page xxx for detailed description and mapping of the fuse high byte. fuse and lock bits that are programmed, will be read as zero. fuse and lock bits that are unprogrammed, will be read as one. bit 76543210 rd ??????lb2lb1 bit 76543210 rd flb7 flb6 flb5 flb4 flb3 flb2 flb1 flb0 bit 76543210 rd fhb7 fhb6 fhb5 fhb4 fhb3 fhb2 fhb1 fhb0
99 attiny13 2535b?avr?01/04 preventing flash corruption during periods of low v cc , the flash program can be corrupted because the supply volt- age is too low for the cpu and the flash to operate properly. these issues are the same as for board level systems using the flash, and the same design solutions should be applied. a flash program corruption can be caused by two situations when the voltage is too low. first, a regular write sequence to the flash requires a minimum voltage to operate cor- rectly. secondly, the cpu itself can execute instructions incorrectly, if the supply voltage for executing instructions is too low. flash corruption can easily be avoided by following these design recommendations (one is sufficient): 1. keep the avr reset active (low) during periods of insufficient power supply voltage. this can be done by enabling the internal brown-out detector (bod) if the operating voltage matches the detection level. if not, an external low v cc reset protection circuit can be used. if a reset occurs while a write operation is in progress, the write operation will be completed provided that the power supply voltage is sufficient. 2. keep the avr core in power-down sleep mode during periods of low v cc . this will prevent the cpu from attempting to decode and execute instructions, effec- tively protecting the spmcsr register and thus the flash from unintentional writes. programming time for flash when using spm the calibrated rc oscillator is used to time flash accesses. table 41 shows the typical programming time for flash accesses from the cpu. table 41. spm programming time symbol min programming time max programming time flash write (page erase, page write, and write lock bits by spm) 3.7 ms 4.5 ms
100 attiny13 2535b?avr?01/04 memory programming this section describes the different methods for programming the attiny13 memories. program and data memory lock bits the attiny13 provides two lock bits which can be left unprogrammed (?1?) or can be programmed (?0?) to obtain the additional security listed in table 43. the lock bits can only be erased to ?1? with the chip erase command. program memory can be read out via the debugwire interface when the dwen fuse is programmed, even if the lock bits are set. thus, when lock bit security is required, should always debugwire be disabled by clearing the dwen fuse. note: 1. ?1? means unprogrammed, ?0? means programmed notes: 1. program the fuse bits before programming the lb1 and lb2. 2. ?1? means unprogrammed, ?0? means programmed table 42. lock bit byte (1) lock bit byte bit no description default value 7 ? 1 (unprogrammed) 6 ? 1 (unprogrammed) 5 ? 1 (unprogrammed) 4 ? 1 (unprogrammed) 3 ? 1 (unprogrammed) 2 ? 1 (unprogrammed) lb2 1 lock bit 1 (unprogrammed) lb1 0 lock bit 1 (unprogrammed) table 43. lock bit protection modes (1)(2) memory lock bits protection type lb mode lb2 lb1 1 1 1 no memory lock features enabled. 210 further programming of the flash and eeprom is disabled in high-voltage and serial programming mode. the fuse bits are locked in both serial and high-voltage programming mode. (1) debugwire is disabled. 300 further programming and verification of the flash and eeprom is disabled in high-voltage and serial programming mode. the fuse bits are locked in both serial and high-voltage programming mode. (1) debugwire is disabled.
101 attiny13 2535b?avr?01/04 fuse bytes the attiny13 has two fuse bytes. table 44 and table 45 describe briefly the functional- ity of all the fuses and how they are mapped into the fuse bytes. note that the fuses are read as logical zero, ?0?, if they are programmed. notes: 1. see table 13 on page 32 for bodl evel fuse decoding. 2. see ?alternate functions of port b? on page 49 for description of rstdisbl and dwen fuses. 3. dwen must be unprogrammed when lock bit security is required. see ?program and data memory lock bits? on page 100. 4. when programming the rstdisbl fuse, high-voltage serial programming has to be used to change fuses to perform further programming. notes: 1. the spien fuse is not accessible in spi programming mode. 2. see ?watchdog timer control register - wdtcr? on page 37 for details. 3. the default value of sut1..0 results in maximum start-up time for the default clock source. see table 5 on page 22 for details. 4. the default setting of cksel1..0 results in internal rc oscillator @ 9.6 mhz. see table 4 on page 22 for details. 5. see ?system clock prescaler? on page 24 for details. the status of the fuse bits is not affected by chip erase. note that the fuse bits are locked if lock bit1 (lb1) is programmed. program the fuse bits before programming the lock bits. table 44. fuse high byte fuse high byte bit no description default value ? 7 ? 1 (unprogrammed) ? 6 ? 1 (unprogrammed) ? 5 ? 1 (unprogrammed) selfprgen 4 self programming enable 1 (unprogrammed) dwen (3) 3 debugwire enable 1 (unprogrammed) bodlevel1 (1) 2 brown-out detector trigger level 1 (unprogrammed) bodlevel0 (1) 1 brown-out detector trigger level 1 (unprogrammed) rstdisbl (4) 0 external reset disable 1 (unprogrammed) table 45. fuse low byte fuse low byte bit no description default value spien (1) 7 enable serial program and data downloading 0 (programmed, spi prog. enabled) eesave 6 eeprom memory is preserved through the chip erase 1 (unprogrammed, eeprom not preserved) wdton (2) 5 watchdog timer always on 1 (unprogrammed) ckdiv8 (5) 4 divide clock by 8 0 (programmed) sut1 3 select start-up time 1 (unprogrammed) (3) sut0 2 select start-up time 0 (programmed) (3) cksel1 1 select clock source 1 (unprogrammed) (4) cksel0 0 select clock source 0 (programmed) (4)
102 attiny13 2535b?avr?01/04 latching of fuses the fuse values are latched when the device enters programming mode and changes of the fuse values will have no effect until the part leaves programming mode. this does not apply to the eesave fuse which will take effect once it is programmed. the fuses are also latched on power-up in normal mode. signature bytes all atmel microcontrollers have a three-byte signature code which identifies the device. this code can be read in both serial and high-voltage programming mode, also when the device is locked. the three bytes reside in a separate address space. for the attiny13 the signature bytes are: 1. 0x000: 0x1e (indicates manufactured by atmel). 2. 0x001: 0x90 (indicates 1 kb flash memory). 3. 0x002: 0x07 (indicates attiny13 device when 0x001 is 0x90). calibration byte signature area of the attiny13 has one byte of calibration data for the internal rc oscil- lator. this byte resides in the high byte of address 0x000. during reset, this byte is automatically written into the osccal register to ensure correct frequency of the cali- brated rc oscillator. page size table 46. no. of words in a page and no. of pages in the flash flash size page size pcword no. of pages pcpage pcmsb 512 words (1k byte) 16 words pc[3:0] 32 pc[8:4] 8 table 47. no. of words in a page and no. of pages in the eeprom eeprom size page size pcword no. of pages pcpage eeamsb 64 bytes 4 bytes eea[1:0] 16 eea[5:2] 5
103 attiny13 2535b?avr?01/04 serial downloading both the flash and eeprom memory arrays can be programmed using the serial spi bus while reset is pulled to gnd. the serial interface consists of pins sck, mosi (input) and miso (output). after reset is set low, the programming enable instruction needs to be executed first before program/erase operations can be executed. note, in table 48 on page 103, the pin mapping for spi programming is listed. not all parts use the spi pins dedicated for the internal spi interface. figure 53. serial programming and verify (1) notes: 1. if the device is clocked by the internal oscillator, it is no need to connect a clock source to the clki pin. when programming the eeprom, an auto-erase cycle is built into the self-timed pro- gramming operation (in the serial mode only) and there is no need to first execute the chip erase instruction. the chip erase operation turns the content of every memory location in both the program and eeprom arrays into 0xff. depending on cksel fuses, a valid clock must be present. the minimum low and high periods for the serial clock (sck) input are defined as follows: low:> 2 cpu clock cycles for f ck < 12 mhz, 3 cpu clock cycles for f ck >= 12 mhz high:> 2 cpu clock cycles for f ck < 12 mhz, 3 cpu clock cycles for f ck >= 12 mhz table 48. pin mapping serial programming symbol pins i/o description mosi pb0 i serial data in miso pb1 o serial data out sck pb2 i serial clock vcc gnd sck mis o mosi reset +1.8 - 5.5v pb0 pb1 pb2 pb5
104 attiny13 2535b?avr?01/04 serial programming algorithm when writing serial data to the attiny13, data is clocked on the rising edge of sck. when reading data from the attiny13, data is clocked on the falling edge of sck. see figure 54 and figure 55 for timing details. to program and verify the attiny13 in the serial programming mode, the following sequence is recommended (see four byte instruction formats in table 50 ): 1. power-up sequence: apply power between v cc and gnd while reset and sck are set to ?0?. in some systems, the programmer can not guarantee that sck is held low during power-up. in this case, reset must be given a positive pulse of at least two cpu clock cycles duration after sck has been set to ?0?. 2. wait for at least 20 ms and enable serial programming by sending the program- ming enable serial instruction to pin mosi. 3. the serial programming instructions will not work if the communication is out of synchronization. when in sync. the second byte (0x53), will echo back when issuing the third byte of the programming enable instruction. whether the echo is correct or not, all four bytes of the instruction must be transmitted. if the 0x53 did not echo back, give reset a positive pulse and issue a new programming enable command. 4. the flash is programmed one page at a time. the memory page is loaded one byte at a time by supplying the 5 lsb of the address and data together with the load program memory page instruction. to ensure correct loading of the page, the data low byte must be loaded before data high byte is applied for a given address. the program memory page is stored by loading the write program memory page instruction with the 3 msb of the address. if polling is not used, the user must wait at least t wd_flash before issuing the next page. (see table 49.) accessing the serial programming interface before the flash write operation completes can result in incorrect programming. 5. a: the eeprom array is programmed one byte at a time by supplying the address and data together with the appropriate write instruction. an eeprom memory location is first automatically erased before new data is written. if polling is not used, the user must wait at least t wd_eeprom before issuing the next byte. (see table 49.) in a chip erased device, no 0xffs in the data file(s) need to be programmed. b: the eeprom array is programmed one page at a time. the memory page is loaded one byte at a time by supplying the 2 lsb of the address and data together with the load eeprom memory page instruction. the eeprom mem- ory page is stored by loading the write eeprom memory page instruction with the 4 msb of the address. when using eeprom page access only byte loca- tions loaded with the load eeprom memory page instruction is altered. the remaining locations remain unchanged. if polling is not used, the used must wait at least t wd_eeprom before issuing the next page (see table 47). in a chip erased device, no 0xff in the data file(s) need to be programmed. 6. any memory location can be verified by using the read instruction which returns the content at the selected address at serial output miso. 7. at the end of the programming session, reset can be set high to commence normal operation. 8. power-off sequence (if needed): set reset to ?1?. tur n v cc power off.
105 attiny13 2535b?avr?01/04 data polling flash when a page is being programmed into the flash, reading an address location within the page being programmed will give the value 0xff. at the time the device is ready for a new page, the programmed value will read correctly. this is used to determine when the next page can be written. note that the entire page is written simultaneously and any address within the page can be used for polling. data polling of the flash will not work for the value 0xff, so when programming this value, the user will have to wait for at least t wd_flash before programming the next page. as a chip-erased device contains 0xff in all locations, programming of addresses that are meant to contain 0xff, can be skipped. see table 49 for t wd_flash value. data polling eeprom when a new byte has been written and is being programmed into eeprom, reading the address location being programmed will give the value 0xff. at the time the device is ready for a new byte, the programmed value will read correctly. this is used to deter- mine when the next byte can be written. this will not work for the value 0xff, but the user should have the following in mind: as a chip-erased device contains 0xff in all locations, programming of addresses that are meant to contain 0xff, can be skipped. this does not apply if the eeprom is re-programmed without chip erasing the device. in this case, data polling cannot be used for the value 0xff, and the user will have to wait at least t wd_eeprom before programming the next byte. see table 49 for t wd_eeprom value. figure 54. serial programming waveforms table 49. minimum wait delay before writing the next flash or eeprom location symbol minimum wait delay t wd_flash 4.5 ms t wd_eeprom 4.0 ms t wd_erase 4.0 ms t wd_fuse 4.5 ms msb msb lsb lsb serial clock input (sck) serial data input (mosi) (miso) sample serial data output
106 attiny13 2535b?avr?01/04 note: a = address high bits, b = address low bits, h = 0 - low byte, 1 - high byte, o = data out, i = data in, x = don?t care table 50. serial programming instruction set instruction instruction format operation byte 1 byte 2 byte 3 byte4 programming enable 1010 1100 0101 0011 xxxx xxxx xxxx xxxx enable serial programming after reset goes low. chip erase 1010 1100 100x xxxx xxxx xxxx xxxx xxxx chip erase eeprom and flash. read program memory 0010 h 000 0000 000 a bbbb bbbb oooo oooo read h (high or low) data o from program memory at word address a:b . load program memory page 0100 h 000 000x xxxx xxx b bbbb iiii iiii write h (high or low) data i to program memory page at word address b . data low byte must be loaded before data high byte is applied within the same address. write program memory page 0100 1100 0000 000 abb xx xxxx xxxx xxxx write program memory page at address a:b . read eeprom memory 1010 0000 000x xxxx xx bb bbbb oooo oooo read data o from eeprom memory at address b . write eeprom memory 1100 0000 000x xxxx xx bb bbbb iiii iiii write data i to eeprom memory at address b . load eeprom memory page (page access) 1100 0001 0000 0000 0000 00 bb iiii iiii load data i to eeprom memory page buffer. after data is loaded, program eeprom page. write eeprom memory page (page access) 1100 0010 00xx xxxx xx bb bb 00 xxxx xxxx write eeprom page at address b . read lock bits 0101 1000 0000 0000 xxxx xxxx xx oo oooo read lock bits. ?0? = programmed, ?1? = unprogrammed. see table 42 on page 100 for details. write lock bits 1010 1100 111x xxxx xxxx xxxx 11 ii iiii write lock bits. set bits = ?0? to program lock bits. see table 42 on page 100 for details. read signature byte 0011 0000 000x xxxx xxxx xx bb oooo oooo read signature byte o at address b . write fuse bits 1010 1100 1010 0000 xxxx xxxx iiii iiii set bits = ?0? to program, ?1? to unprogram. write fuse high bits 1010 1100 1010 1000 xxxx xxxx iiii iiii set bits = ?0? to program, ?1? to unprogram. see table 36 on page 82 for details. read fuse bits 0101 0000 0000 0000 xxxx xxxx oooo oooo read fuse bits. ?0? = programmed, ?1? = unprogrammed. read fuse high bits 0101 1000 0000 1000 xxxx xxxx oooo oooo read fuse high bits. ?0? = pro- grammed, ?1? = unprogrammed. see table 36 on page 82 for details. read calibration byte 0011 1000 000x xxxx 0000 0000 oooo oooo read calibration byte poll rdy/bsy 1111 0000 0000 0000 xxxx xxxx xxxx xxx o if o = ?1?, a programming operation is still busy. wait until this bit returns to ?0? before applying another command.
107 attiny13 2535b?avr?01/04 serial programming characteristics figure 55. serial programming timing note: 1. 2 t clcl for f ck < 12 mhz, 3 t clcl for f ck >= 12 mhz high-voltage serial programming this section describes how to program and verify flash program memory, eeprom data memory, lock bits and fuse bits in the attiny13. table 51. serial programming characteristics, t a = -40 c to 85 c, v cc = 1.8 - 5.5v (unless otherwise noted) symbol parameter min typ max units 1/t clcl oscillator frequency (attiny13v) 0 1 mhz t clcl oscillator period (attiny13v) 1,000 ns 1/t clcl oscillator frequency (attiny13l, vcc = 2.7 - 5.5v) 0 9.6 mhz t clcl oscillator period (attiny13l, vcc = 2.7 - 5.5v) 104 ns 1/t clcl oscillator frequency (attiny13, v cc = 4.5v - 5.5v) 0 16 mhz t clcl oscillator period (attiny13, v cc = 4.5v - 5.5v) 67 ns t shsl sck pulse width high 2 t clcl * ns t slsh sck pulse width low 2 t clcl * ns t ovsh mosi setup to sck high t clcl ns t shox mosi hold after sck high 2 t clcl ns t sliv sck low to miso valid tbd tbd tbd ns mosi miso sck t ovsh t shsl t slsh t shox t sliv
108 attiny13 2535b?avr?01/04 figure 56. high-voltage serial programming the minimum period for the serial clock input (sci) during high-voltage serial pro- gramming is 220 ns. table 52. pin name mapping signal name in high-voltage serial programming mode pin name i/o function sdi pb0 i serial data input sii pb1 i serial instruction input sdo pb2 o serial data output sci pb3 i serial clock input (min. 220ns period) table 53. pin values used to enter programming mode pin symbol value sdi prog_enable[0] 0 sii prog_enable[1] 0 sdo prog_enable[2] 0 vcc gnd sck miso mosi (reset) +1.8 - 5.5v pb0 pb1 pb2 pb5 +11.5 - 12.5v pb3 (clki) serial clock input
109 attiny13 2535b?avr?01/04 high-voltage serial programming algorithm to program and verify the attiny13 in the high-voltage serial programming mode, the following sequence is recommended (see instruction formats in table 55): enter high-voltage serial programming mode the following algorithm puts the device in high-voltage serial programming mode: 1. apply 4.5 - 5.5v between v cc and gnd. 2. set reset pin to ?0? and toggle sci at least six times. 3. set the prog_enable pins listed in table 53 to ?000? and wait at least 100 ns. 4. apply v hvrst - 5.5v to reset. keep the prog_enable pins unchanged for at least t hvrst after the high-voltage has been applied to ensure the prog_enable signature has been latched. 5. shortly after latching the prog_enable signature, the device will actively output data on the prog_enable[2]/sdo pin, and the resulting drive contention may increase the power consumption. to minimize this drive contention, release the prog_enable[2] pin after t hvrst has elapsed. 6. wait at least 50 s before giving any serial instructions on sdi/sii. considerations for efficient programming the loaded command and address are retained in the device during programming. for efficient programming, the following should be considered.  the command needs only be loaded once when writing or reading multiple memory locations.  skip writing the data value 0xff that is the contents of the entire eeprom (unless the eesave fuse is programmed) and flash after a chip erase.  address high byte needs only be loaded before programming or reading a new 256 word window in flash or 256 byte eeprom. this consideration also applies to signature bytes reading. chip erase the chip erase will erase the flash and eeprom (1) memories plus lock bits. the lock bits are not reset until the program memory has been completely erased. the fuse bits are not changed. a chip erase must be performed before the flash and/or eeprom are re-programmed. note: 1. the eeprom memory is preserved during chip erase if the eesave fuse is programmed. 1. load command ?chip erase? (see table 55). 2. wait after instr. 3 until sdo goes high for the ?chip erase? cycle to finish. 3. load command ?no operation?. table 54. high-voltage reset characteristics supply voltage reset pin high-voltage threshold minimum high-voltage period for latching prog_enable v cc v hvrst t hvrst 4.5v 11.5v 100 ns 5.5v 11.5v 100 ns
110 attiny13 2535b?avr?01/04 programming the flash the flash is organized in pages, see table 50 on page 106. when programming the flash, the program data is latched into a page buffer. this allows one page of program data to be programmed simultaneously. the following procedure describes how to pro- gram the entire flash memory: 1. load command ?write flash? (see table 55). 2. load flash page buffer. 3. load flash high address and program page. wait after instr. 3 until sdo goes high for the ?page programming? cycle to finish. 4. repeat 2 through 3 until the entire flash is programmed or until all data has been programmed. 5. end page programming by loading command ?no operation?. when writing or reading serial data to the attiny13, data is clocked on the rising edge of the serial clock, see figure 58, figure 59 and table 56 for details. figure 57. addressing the flash which is organized in pages figure 58. high-voltage serial programming waveforms program memory word address within a page page address within the flash instruction word pag e pcword[pagemsb:0]: 00 01 02 pageend pag e pcword pcpage pcmsb pagemsb program counter msb msb msb lsb lsb lsb 012345678910 sdi pb0 sii pb1 sdo pb2 sci pb3
111 attiny13 2535b?avr?01/04 programming the eeprom the eeprom is organized in pages, see table 51 on page 107. when programming the eeprom, the data is latched into a page buffer. this allows one page of data to be programmed simultaneously. the programming algorithm for the eeprom data mem- ory is as follows (refer to table 55): 1. load command ?write eeprom?. 2. load eeprom page buffer. 3. program eeprom page. wait after instr. 2 until sdo goes high for the ?page programming? cycle to finish. 4. repeat 2 through 3 until the entire eeprom is programmed or until all data has been programmed. 5. end page programming by loading command ?no operation?. reading the flash the algorithm for reading the flash memory is as follows (refer to table 55): 1. load command "read flash". 2. read flash low and high bytes. the contents at the selected address are avail- able at serial output sdo. reading the eeprom the algorithm for reading the eeprom memory is as follows (refer to table 55): 1. load command ?read eeprom?. 2. read eeprom byte. the contents at the selected address are available at serial output sdo. programming and reading the fuse and lock bits the algorithms for programming and reading the fuse low/high bits and lock bits are shown in table 55. reading the signature bytes and calibration byte the algorithms for reading the signature bytes and calibration byte are shown in table 55. power-off sequence set sci to ?0?. set reset to ?1?. turn v cc power off.
112 attiny13 2535b?avr?01/04 table 55. high-voltage serial programming instruction set for attiny13 instruction instruction format operation remarks instr.1/5 instr.2/6 instr.3 instr.4 chip erase sdi sii sdo 0_1000_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx wait after instr.3 until sdo goes high for the chip erase cycle to finish. load ?write flash? command sdi sii sdo 0_0001_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx enter flash programming code. load flash page buffer sdi sii sdo 0_ bbbb _ bbbb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_ eeee _ eeee _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_ dddd _ dddd _00 0_0011_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1101_00 x_xxxx_xxxx_xx repeat after instr. 1 - 5 until the entire page buffer is filled or until all data within the page is filled. see note 1. sdi sii sdo 0_0000_0000_00 0_0111_1100_00 x_xxxx_xxxx_xx instr 5. load flash high address and program page sdi sii sdo 0_0000_000 a _00 0_0001_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx wait after instr 3 until sdo goes high. repeat instr. 2 - 3 for each loaded flash page until the entire flash or all data is programmed. repeat instr. 1 for a new 256 byte page. see note 1. load ?read flash? command sdi sii sdo 0_0000_0010_00 0_0100_1100_00 x_xxxx_xxxx_xx enter flash read mode. read flash low and high bytes sdi sii sdo 0_ bbbb _ bbbb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_0000_000 a _00 0_0001_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 q _ qqqq _ qqq x_xx repeat instr. 1, 3 - 6 for each new address. repeat instr. 2 for a new 256 byte page. sdi sii sdo 0_0000_0000_00 0_0111_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1100_00 p _ pppp _ ppp x_xx instr 5 - 6. load ?write eeprom? command sdi sii sdo 0_0001_0001_00 0_0100_1100_00 x_xxxx_xxxx_xx enter eeprom programming mode. load eeprom page buffer sdi sii sdo 0_00 bb _ bbbb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_ eeee _ eeee _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1101_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx repeat instr. 1 - 4 until the entire page buffer is filled or until all data within the page is filled. see note 2. program eeprom page sdi sii sdo 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx wait after instr. 2 until sdo goes high. repeat instr. 1 - 2 for each loaded eeprom page until the entire eeprom or all data is programmed. write eeprom byte sdi sii sdo 0_00 bb _ bbbb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_ eeee _ eeee _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1101_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx repeat instr. 1 - 5 for each new address. wait after instr. 5 until sdo goes high. see note 3. sdi sii sdo 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx instr. 5
113 attiny13 2535b?avr?01/04 note: a = address high bits, b = address low bits, d = data in high bits, e = data in low bits, p = data out high bits, q = data out low bits, x = don?t care, 1 = lock bit1, 2 = lock bit2, 3 = cksel0 fuse, 4 = cksel1 fuse, 5 = sut0 fuse, 6 = sut1 fuse, 7 = ckdiv8, fuse, 8 = wdton fuse, 9 = eesave fuse, a = spien fuse, b = rstdisbl fuse, c = bodlevel0 fuse, d = bodlevel1 fuse, e = monen fuse, f = selfprgen fuse notes: 1. for page sizes less than 256 words, parts of the address (bbbb_bbbb) will be parts of the page address. 2. for page sizes less than 256 bytes, parts of the address (bbbb_ bbbb) will be parts of the page address. 3. the eeprom is written page-wise. but only the bytes that are loaded into the page are actually written to the eeprom. page-wise eeprom access is more efficient when multiple bytes are to be written to the same page. note that auto-erase of eeprom is not available in high-voltage serial programming, only in spi programming. load ?read eeprom? command sdi sii sdo 0_0000_0011_00 0_0100_1100_00 x_xxxx_xxxx_xx enter eeprom read mode. read eeprom byte sdi sii sdo 0_ bbbb _ bbbb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_ aaaa _ aaaa _00 0_0001_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 q _ qqqq _ qqq 0_00 repeat instr. 1, 3 - 4 for each new address. repeat instr. 2 for a new 256 byte page. write fuse low bits sdi sii sdo 0_0100_0100_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_ a987 _ 6543 _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx wait after instr. 4 until sdo goes high. write a - 3 = ?0? to program the fuse bit. write fuse high bits sdi sii sdo 0_0100_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_000 f _ edcb _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1100_00 x_xxxx_xxxx_xx wait after instr. 4 until sdo goes high. write f - b = ?0? to program the fuse bit. write lock bits sdi sii sdo 0_0010_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_00 21 _00 0_0010_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_0100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 x_xxxx_xxxx_xx wait after instr. 4 until sdo goes high. write 2 - 1 = ?0? to program the lock bit. read fuse low bits sdi sii sdo 0_0000_0100_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 a _ 9876 _ 543 x_xx reading a - 3 = ?0? means the fuse bit is programmed. read fuse high bits sdi sii sdo 0_0000_0100_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1010_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1110_00 x_xx fe _ dcb x_xx reading f - b = ?0? means the fuse bit is programmed. read lock bits sdi sii sdo 0_0000_0100_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1100_00 x_xxxx_x 21 x_xx reading 2, 1 = ?0? means the lock bit is programmed. read signature bytes sdi sii sdo 0_0000_1000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_00 bb _00 0_0000_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0110_1100_00 q _ qqqq _ qqq x_xx repeats instr 2 4 for each signature byte address. read calibration byte sdi sii sdo 0_0000_1000_00 0_0100_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0000_1100_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1000_00 x_xxxx_xxxx_xx 0_0000_0000_00 0_0111_1100_00 p _ pppp _ ppp x_xx load ?no operation? command sdi sii sdo 0_0000_0000_00 0_0100_1100_00 x_xxxx_xxxx_xx table 55. high-voltage serial programming instruction set for attiny13 (continued) instruction instruction format operation remarks instr.1/5 instr.2/6 instr.3 instr.4
114 attiny13 2535b?avr?01/04 high-voltage serial programming characteristics figure 59. high-voltage serial programming timing table 56. high-voltage serial programming characteristics v cc = 5.0v 10% (unless otherwise noted) symbol parameter min typ max units t shsl sci (pb3) pulse width high 110 ns t slsh sci (pb3) pulse width low 110 ns t ivsh sdi (pb0), sii (pb1) valid to sci (pb3) high 50 ns t shix sdi (pb0), sii (pb1) hold after sci (pb3) high 50 ns t shov sci (pb3) high to sdo (pb2) valid 16 ns t wlwh_pfb wait after instr. 3 for write fuse bits 2.5 ms sdi (pb0), sii (pb1) sdo (pb2) sci (pb3) t ivsh t shsl t slsh t shix t shov
115 attiny13 2535b?avr?01/04 electrical characteristics absolute maximum ratings* dc characteristics notes: 1. all dc characteristics contained in this data sheet are based on simulation and characterization of other avr microcont rol- lers manufactured in the same process technology. these values are preliminary values representing design targets, and will be updated after characterization of actual silicon. 2. ?max? means the highest value where t he pin is guaranteed to be read as low. operating temperature .................................. -55 c to +125 c *notice: stresses beyond those listed under ?absolute maximum ratings? may cause permanent dam- age to the device. this is a stress rating only and functional operation of the device at these or other conditions beyond those indicated in the operational sections of this specification is not implied. exposure to absolute maximum rating conditions for extended periods may affect device reliability. storage temperature ..................................... -65c to +150c voltage on any pin except reset with respect to ground ................................-0.5v to v cc +0.5v voltage on reset with respect to ground......-0.5v to +13.0v maximum operating voltage ............................................ 6.0v dc current per i/o pin ............................................... 40.0 ma dc current v cc and gnd pins................................ 200.0 ma t a = -40 c to 85 c, v cc = 1.8v to 5.5v (unless otherwise noted) (1) symbol parameter condition min. typ. max. units v il input low voltage -0.5 0.2v cc v v ih input high-voltage except reset pin 0.6v cc (3) v cc +0.5 v v ih2 input high-voltage reset pin 0.9v cc (3) v cc +0.5 v v ol output low voltage (4) (port b) i ol = 10 ma, v cc = 5v i ol = 5 ma, v cc = 3v 0.7 0.5 v v v oh output high-voltage (5) (port b) i oh = -10 ma, v cc = 5v i oh = -5 ma, v cc = 3v 4.2 2.5 v v i il input leakage current i/o pin vcc = 5.5 v, p i n l o w (absolute value) 1a i ih input leakage current i/o pin vcc = 5.5 v, pin high (absolute value) 1a r rst reset pull-up resistor 30 80 k ? r pu i/o pin pull-up resistor 20 50 k ? i cc power supply current active 1mhz, v cc = 2v 0.55 ma active 4mhz, v cc = 3v 3.5 ma active 8mhz, v cc = 5v 12 ma idle 1mhz, v cc = 2v 0.08 0.25 ma idle 4mhz, v cc = 3v 0.41 1.5 ma idle 8mhz, v cc = 5v 1.6 5.5 ma power-down mode wdt enabled, v cc = 3v < 5 16 a wdt disabled, v cc = 3v < 0.5 8 a
116 attiny13 2535b?avr?01/04 3. ?min? means the lowest value where the pin is guaranteed to be read as high. 4. although each i/o port can sink more t han the test conditions (10 ma at v cc = 5v, 5 ma at v cc = 3v) under steady state conditions (non-transient), the following must be observed: 1] the sum of all iol, for all ports, should not exceed 60 ma. if iol exceeds the test condition, vol may exceed the related specification. pins are not guaranteed to sink current greater than the listed test condition. 5. although each i/o port can source more than the test conditions (10 ma at v cc = 5v, 5 ma at v cc = 3v) under steady state conditions (non-transient), the following must be observed: 1] the sum of all ioh, for all ports, should not exceed 60 ma. if ioh exceeds the test condition, voh may exceed the related specification. pins are not guaranteed to source current greater than the listed test condition. external clock drive waveforms figure 60. external clock drive waveforms external clock drive maximum speed vs. v cc maximum frequency is depending on v cc. as shown in figure 61 and figure 62, the maximum frequency vs. v cc curve is linear between 1.8v < v cc < 4.5v. to calculate the maximum frequency at a given voltage in this interval, use this equation: at 3 volt, this gives: thus, when v cc = 3v, maximum frequency will be 14 mhz. v il1 v ih1 table 57. external clock drive symbol parameter v cc = 1.8 - 5.5v v cc = 2.7 - 5.5v v cc = 4.5 - 5.5v units min. max. min. max. min. max. 1/t clcl clock frequency 0 1 0 9.6 0 16 mhz t clcl clock period 1000 104 62.5 ns t chcx high time 400 50 25 ns t clcx low time 400 50 25 ns t clch rise time 2.0 1.6 0.5 s t chcl fall time 2.0 1.6 0.5 s ? t clcl change in period from one clock cycle to the next 2 2 2 % frequency v 0.9 ? () 0.15 ---------------------- = frequency 30.9 ? () 0.15 ---------------------- 14 ==
117 attiny13 2535b?avr?01/04 to calculate required voltage for a maximum frequency, use this equation:: at 19 mhz this gives: thus, a maximum frequency of 19 mhz requires v cc = 3.75 v. figure 61. maximum frequency vs. v cc , attiny13v figure 62. maximum frequency vs. v cc , attiny13 voltage 0.9 0.15 f ? + = voltage 0.9 0.15 19 ? + 3.75 v == 12 mhz 6 mhz 1.8v 2.7v 5.5v safe operating area 24 mhz 12 mhz 2.7v 4.5v 5.5v safe operating area
118 attiny13 2535b?avr?01/04 adc characteristics ? preliminary data notes: 1. values are preliminary. table 58. adc characteristics, single ended channels. -40 c - 85 c symbol parameter condition min (1) typ (1) max (1) units resolution single ended conversion 10 bits absolute accuracy (including inl, dnl, quantization error, gain and offset error) single ended conversion v ref = 4v, v cc = 4v, adc clock = 200 khz 2lsb single ended conversion v ref = 4v, v cc = 4v, adc clock = 1 mhz 3lsb single ended conversion v ref = 4v, v cc = 4v, adc clock = 200 khz noise reduction mode 1.5 lsb single ended conversion v ref = 4v, v cc = 4v, adc clock = 1 mhz noise reduction mode 2.5 lsb integral non-linearity (inl) single ended conversion v ref = 4v, v cc = 4v, adc clock = 200 khz 1lsb differential non-linearity (dnl) single ended conversion v ref = 4v, v cc = 4v, adc clock = 200 khz 0.5 lsb gain error single ended conversion v ref = 4v, v cc = 4v, adc clock = 200 khz 2.5 lsb offset error single ended conversion v ref = 4v, v cc = 4v, adc clock = 200 khz 1.5 lsb conversion time free running conversion 13 260 s clock frequency 50 1000 khz v in input voltage gnd v ref v input bandwidth 38.5 khz v int internal voltage reference 1.0 1.1 1.2 v r ain analog input resistance 100 m ?
119 attiny13 2535b?avr?01/04 attiny13 typical characteristics ? preliminary data the following charts show typical behavior. these figures are not tested during manu- facturing. all current consumption measurements are performed with all i/o pins configured as inputs and with internal pull-ups enabled. a sine wave generator with rail- to-rail output is used as clock source. the power consumption in power-down mode is independent of clock selection. the current consumption is a function of several factors such as: operating voltage, operating frequency, loading of i/o pins, switching rate of i/o pins, code executed and ambient temperature. the dominating factors are operating voltage and frequency. the current drawn from capacitive loaded pins may be estimated (for one pin) as c l * v cc *f where c l = load capacitance, v cc = operating voltage and f = average switch- ing frequency of i/o pin. the parts are characterized at frequencies higher than test limits. parts are not guaran- teed to function properly at frequencies higher than the ordering code indicates. the difference between current consumption in power-down mode with watchdog timer enabled and power-down mode with watchdog timer disabled represents the dif- ferential current drawn by the watchdog timer. active supply current figure 63. active supply current vs. frequency (0.1 - 1.0 mhz) active supply current vs. low frequency 0.1 - 1.0 mh z 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0.2 0.4 0.6 0.8 1 1.2 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 frequency (mhz) i cc (ma)
120 attiny13 2535b?avr?01/04 figure 64. active supply current vs. frequency (1 - 24 mhz) figure 65. active supply current vs. v cc (internal rc oscillator, 9.6 mhz) active supply current vs. frequency 1 - 24 mh z 0 2 4 6 8 10 12 14 16 18 02468 1012141618202224 frequency (mhz) i cc (ma) 5.5v 5.0v 4.5v 4.0v 3.3v 2.7v 1.8v active supply current vs. v cc internal rc oscillator, 9.6 mh z 85 c 25 c -40 c 0 1 2 3 4 5 6 7 8 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma)
121 attiny13 2535b?avr?01/04 figure 66. active supply current vs. v cc (internal rc oscillator, 4.8 mhz) figure 67. active supply current vs. v cc (internal wdt oscillator, 128 khz) active supply current vs. v cc internal rc oscillator, 4.8 mh z 85 c 25 c -40 c 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma) active supply current vs. v cc internal wd oscillator, 128 khz 85 c 25 c -40 c 0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma)
122 attiny13 2535b?avr?01/04 figure 68. active supply current vs. v cc (32 khz external clock) idle supply current figure 69. idle supply current vs. frequency (0.1 - 1.0 mhz) active supply current vs. v cc 32 khz external clock 85 c 25 c 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma) idle supply current vs. low frequency (0.1 - 1.0 mhz) 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 frequency (mhz) i cc (ma)
123 attiny13 2535b?avr?01/04 figure 70. idle supply current vs. frequency (1 - 24 mhz) figure 71. idle supply current vs. v cc (internal rc oscillator, 9.6 mhz) idle supply current vs. frequency 1 - 24 mh z 0 2 4 6 8 10 12 0 2 4 6 8 10 12 14 16 18 20 22 24 frequency (mhz) i cc (ma) 1.8v 2.7v 3.3v 4.0v 4.5v 5.0v 5.5v idle supply current vs. v cc internal rc oscillator, 9.6 mh z 85 c 25 c -40 c 0 0.5 1 1.5 2 2.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma)
124 attiny13 2535b?avr?01/04 figure 72. idle supply current vs. v cc (internal rc oscillator, 4.8 mhz) figure 73. idle supply current vs. v cc (internal rc oscillator, 128 khz) idle supply current vs. v cc internal rc oscillator, 4.8 mh z 85 c 25 c -40 c 0 0.2 0.4 0.6 0.8 1 1.2 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma) idle supply current vs. v cc internal wd oscillator, 128 kh z 85 c 25 c -40 c 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma)
125 attiny13 2535b?avr?01/04 figure 74. idle supply current vs. v cc (32 khz external clock) power-down supply current figure 75. power-down supply current vs. v cc (watchdog timer disabled) idle supply current vs. v cc 32khz external clock 85 c 25 c -40 c 0 1 2 3 4 5 6 7 8 9 10 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ua) power-down supply current vs. v cc watchdog timer disabled 85 ?c 25 ?c -40 ?c 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ua)
126 attiny13 2535b?avr?01/04 figure 76. power-down supply current vs. v cc (watchdog timer enabled) pin pull-up figure 77. i/o pin pull-up resistor current vs. input voltage (v cc = 5v) power-down supply current vs. v cc watchdog timer enabled 85 ?c 25 ?c -40 ?c 0 1 2 3 4 5 6 7 8 9 10 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ua) i/o pin pull-up resistor current vs. input voltage v cc = 5v 25 ?c -40 ?c 0 20 40 60 80 100 120 140 160 0123456 v op (v) i op (ua) 85 ?c
127 attiny13 2535b?avr?01/04 figure 78. i/o pin pull-up resistor current vs. input voltage (v cc = 2.7v) figure 79. reset pull-up resistor current vs. reset pin voltage (v cc = 5v) i/o pin pull-up resistor current vs. input voltage v cc = 2.7 25 ?c -40 ?c 0 10 20 30 40 50 60 70 80 0 0.5 1 1.5 2 2.5 3 v op (v) i op (ua) 85 ? reset pull-up resistor current vs. reset pin voltage v cc = 5v 85 ?c 25 ?c -40 ?c 0 20 40 60 80 100 120 012345 v reset (v) i reset (ua)
128 attiny13 2535b?avr?01/04 figure 80. reset pull-up resistor current vs. reset pin voltage (v cc = 2.7v) pin driver strength figure 81. i/o pin source current vs. output voltage (low power ports, v cc = 5v) reset pull-up resistor current vs. reset pin voltage v cc = 2.7v 85 ?c 25 ?c -40 ?c 0 10 20 30 40 50 60 0 0.5 1 1.5 2 2.5 3 v reset (v) i reset (ua) i/o pin source current vs. output voltage low power ports, v cc = 5v 85 ?c 25 ?c -40 ?c 0 10 20 30 40 50 60 70 0123456 v oh (v) i oh (ma)
129 attiny13 2535b?avr?01/04 figure 82. i/o pin source current vs. output voltage (low power ports, v cc = 2.7v) figure 83. i/o pin source current vs. output voltage (low power ports, v cc = 1.8v) i/o pin source current vs. output voltage low power ports, v cc = 2.7v 85 c 25 c -40 c 0 5 10 15 20 25 0 0.5 1 1.5 2 2.5 3 v oh (v) i oh (ma) i/o pin source current vs. output voltage low power ports, v cc = 1.8v 85 ?c 25 ?c -40 ?c 0 1 2 3 4 5 6 7 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 v oh (v) i oh (ma)
130 attiny13 2535b?avr?01/04 figure 84. i/o pin sink current vs. output voltage (low power ports, v cc = 5v) figure 85. i/o pin sink current vs. output voltage (low power ports, v cc = 2.7v) i/o pin sink current vs. output voltage low power ports, v cc = 5v 85 ?c 25 ?c -40 ?c 0 5 10 15 20 25 30 35 40 45 50 0 0.5 1 1.5 2 2.5 v ol (v) i ol (ma) i/o pin sink current vs. output voltage low power ports, v cc = 2.7v 85 ?c 25 ?c -40 ?c 0 2 4 6 8 10 12 14 16 18 20 0 0.5 1 1.5 2 2.5 v ol (v) i ol (ma)
131 attiny13 2535b?avr?01/04 figure 86. i/o pin sink current vs. output voltage (low power ports, v cc = 1.8v) figure 87. i/o pin source current vs. output voltage (v cc = 5v) i/o pin sink current vs. output voltage low power ports, 1.8v 85 ?c 25 ?c -40 ?c 0 1 2 3 4 5 6 7 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 v ol (v) i ol (ma) i/o pin source current vs. output voltage v cc = 5v 85 ?c 25 ?c -40 ?c 0 10 20 30 40 50 60 70 80 90 23456 v oh (v) i oh (ma)
132 attiny13 2535b?avr?01/04 figure 88. i/o pin source current vs. output voltage (v cc = 2.7v) figure 89. i/o pin source current vs. output voltage (v cc = 1.8v) i/o pin source current vs. output voltage v cc = 2.7v 85 ?c 25 ?c -40 ?c 0 5 10 15 20 25 30 35 0 0.5 1 1.5 2 2.5 3 v oh (v) i oh (ma) i/o pin source current vs. output voltage v cc = 1.8v 85 ?c 25 ?c -40 ?c 0 1 2 3 4 5 6 7 8 9 10 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 v oh (v) i oh (ma)
133 attiny13 2535b?avr?01/04 figure 90. i/o pin sink current vs. output voltage (v cc = 5v) figure 91. i/o pin sink current vs. output voltage (v cc = 2.7v) i/o pin sink current vs. output voltage v cc = 5v 85 ?c 25 ?c -40 ?c 0 10 20 30 40 50 60 70 80 90 100 0 0.5 1 1.5 2 2.5 v ol (v) i ol (ma) i/o pin sink current vs. output voltage v cc = 2.7v 85 ?c 25 ?c -40 ?c 0 5 10 15 20 25 30 35 40 0 0.5 1 1.5 2 2.5 v ol (v) i ol (ma)
134 attiny13 2535b?avr?01/04 figure 92. i/o pin sink current vs. output voltage (v cc = 1.8v) figure 93. reset pin as i/o - source current vs. output voltage (v cc = 5v) i/o pin sink current vs. output voltage v cc = 1.8v 85 ?c 25 ?c -40 ?c 0 2 4 6 8 10 12 14 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 v ol (v) i ol (ma) reset pin as i/o - source current vs. output voltage v cc = 5v 85 ?c 25 ?c -40 ?c 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 2345 v oh (v) i oh (ma)
135 attiny13 2535b?avr?01/04 figure 94. reset pin as i/o - source current vs. output voltage (v cc = 2.7v) figure 95. reset pin as i/o - source current vs. output voltage (v cc = 1.8v) reset pin as i/o - source current vs. output voltage v cc = 2.7v 85 ?c 25 ?c -40 ?c 0 0.5 1 1.5 2 2.5 0 0.5 1 1.5 2 2.5 3 v oh (v) i oh (ma) reset pin as i/o - source current vs. output voltage v cc = 1.8v 85 c 25 c -40 c 0 0.5 1 1.5 2 2.5 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 v oh (v) i oh (ma)
136 attiny13 2535b?avr?01/04 figure 96. reset pin as i/o - sink current vs. output voltage (v cc = 5v) figure 97. reset pin as i/o - sink current vs. output voltage (v cc = 2.7v) reset pin as i/o - sink current vs. output voltage v cc = 5v 85 ?c 25 ?c -40 ?c 0 2 4 6 8 10 12 14 0 0.5 1 1.5 2 2.5 v ol (v) i ol (ma) reset pin as i/o - sink current vs. output voltage v cc = 2.7v 85 ?c 25 ?c -40 ?c 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 0 0.5 1 1.5 2 2.5 v ol (v) i ol (ma)
137 attiny13 2535b?avr?01/04 figure 98. reset pin as i/o - sink current vs. output voltage (v cc = 1.8v) pin thresholds and hysteresis figure 99. i/o pin input threshold voltage vs. v cc (vih, i/o pin read as '1') reset pin as i/o - sink current vs. output voltage v cc = 1.8v 85 ?c 25 ?c -40 ?c 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 v ol (v) i ol (ma) i/o pin input threshold voltage vs. v cc vih, io pin read as '1' 85 ?c 25 ?c -40 ?c 0 0.5 1 1.5 2 2.5 3 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) threshold (v)
138 attiny13 2535b?avr?01/04 figure 100. i/o pin input threshold voltage vs. v cc (vil, i/o pin read as '0') figure 101. i/o pin input hysteresis vs. v cc i/o pin input threshold voltage vs. v cc vil, io pin read as '0' 85 ?c 25 ?c -40 ?c 0 0.5 1 1.5 2 2.5 3 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) threshold (v) i/o pin input hysteresis vs. v cc 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) input hysteresis (v) -40 ? 25 ? 85 ?
139 attiny13 2535b?avr?01/04 figure 102. reset pin as i/o - input threshold voltage vs. v cc (vih, reset pin read as '1') figure 103. reset pin as i/o - input threshold voltage vs. v cc (vil, reset pin read as '0') reset pin as i/o - threshold voltage vs. v cc vih, io pin read as '1' 85 ?c 25 ?c -40 ?c 0 0.5 1 1.5 2 2.5 3 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) threshold (v) reset pin as i/o - threshold voltage vs. v cc vil, io pin read as '0' 85 ?c 25 ?c -40 ?c 0 0.5 1 1.5 2 2.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) threshold (v)
140 attiny13 2535b?avr?01/04 figure 104. reset pin as i/o - pin hysteresis vs. v cc figure 105. reset input threshold voltage vs. v cc (vih, reset pin read as '1') reset pin as io - pin hysteresis vs. v cc 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) input hysteresis (v) -40 ? 25 ? 85 ? reset input threshold voltage vs. v cc vih, io pin read as '1' 85 ?c 25 ?c -40 ?c 0 0.5 1 1.5 2 2.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) threshold (v)
141 attiny13 2535b?avr?01/04 figure 106. reset input threshold voltage vs. v cc (vil, reset pin read as '0') figure 107. reset input pin hysteresis vs. v cc reset input threshold voltage vs. v cc vil, io pin read as '0' 85 ?c 25 ?c -40 ?c 0 0.5 1 1.5 2 2.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) threshold (v) reset input threshold voltage vs. v cc vil, io pin read as '0' 0 0.1 0.2 0.3 0.4 0.5 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) threshold (v) -40 ? 25 ? 85 ?
142 attiny13 2535b?avr?01/04 bod thresholds and analog comparator offset figure 108. bod thresholds vs. temperature (bodlevel is 4.3v) figure 109. bod thresholds vs. temperature (bodlevel is 2.7v) bod thresholds vs. temperature bodlevel is 4.3 v 4.2 4.3 4.4 4.5 -60 -40 -20 0 20 40 60 80 100 temperature (c) threshold (v) rising v cc falling v cc bod thresholds vs. temperature bodlevel is 2.7 v 2.6 2.7 2.8 2.9 -60 -40 -20 0 20 40 60 80 100 temperature (c) threshold (v) falling v cc rising v cc
143 attiny13 2535b?avr?01/04 figure 110. bod thresholds vs. temperature (bodlevel is 1.8v) figure 111. bandgap voltage vs. v cc bod thresholds vs. temperature bodlevel is 1.8 v 1.75 1.8 1.85 1.9 -60 -40 -20 0 20 40 60 80 100 temperature (c) threshold (v) falling v cc rising v cc bandgap voltage vs. v cc 0.92 0.94 0.96 0.98 1 1.02 1.04 1.06 1.5 2.5 3.5 4.5 5.5 v cc (v) bandgap voltage (v) 85oc 25oc -40oc
144 attiny13 2535b?avr?01/04 figure 112. analog comparator offset voltage vs. common mode voltage (v cc = 5v) figure 113. analog comparator offset voltage vs. common mode voltage (v cc = 2.7v) analog comparator offset vs. common mode voltage v cc = 5v 85 c 25 c -40 c 0 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 common mode voltage (v) comparator offset voltage (v) analog comparator offset vs. common mode voltage v cc = 2.7v 85 c 25 c -40 c 0 0.0005 0.001 0.0015 0.002 0.0025 0.003 0 0.5 1 1.5 2 2.5 3 common mode voltage (v) comparator offset voltage (v)
145 attiny13 2535b?avr?01/04 internal oscillator speed figure 114. calibrated 9.6 mhz rc oscillator frequency vs. temperature figure 115. calibrated 9.6 mhz rc oscillator frequency vs. v cc calibrated 9.6 mhz rc oscillator frequency vs. temperature 5.5 v 4.5 v 2.7 v 1.8 v 8.5 8.7 8.9 9.1 9.3 9.5 9.7 9.9 10.1 10.3 -60 -40 -20 0 20 40 60 80 100 temperature (c) f rc (mhz) calibrated 9.6 mhz rc oscillator frequency vs. v cc 85 ?c 25 ?c -40 ?c 8 8.5 9 9.5 10 10.5 11 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) f rc (mhz)
146 attiny13 2535b?avr?01/04 figure 116. calibrated 9.6 mhz rc oscillator frequency vs. osccal value figure 117. calibrated 4.8 mhz rc oscillator frequency vs. temperature calibrated 9.6mhz rc oscillator frequency vs. osccal value 25 ?c 2 4 6 8 10 12 14 16 18 0 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 osccal value f rc (mhz) calibrated 4.8 mhz rc oscillator frequency vs. temperature 5.5 v 4.0 v 2.7 v 1.8 v 4.5 4.6 4.7 4.8 4.9 5 5.1 -60 -40 -20 0 20 40 60 80 100 temperature (c) f rc (mhz)
147 attiny13 2535b?avr?01/04 figure 118. calibrated 4.8 mhz rc oscillator frequency vs. v cc figure 119. calibrated 4.8 mhz rc oscillator frequency vs. osccal value calibrated 4.8 mhz rc oscillator frequency vs. v cc 85 ?c 25 ?c -40 ?c 4.4 4.6 4.8 5 5.2 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) f rc (mhz) calibrated 4.8 mhz rc oscillator frequency vs. osccal value 25 ?c 1 2 3 4 5 6 7 8 9 10 0 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 127 osccal value f rc (mhz)
148 attiny13 2535b?avr?01/04 figure 120. 128 khz watchdog oscillator frequency vs. v cc figure 121. 128 khz watchdog oscillator frequency vs. temperature 128 khz watchdog oscillator frequency vs. v cc 85 ?c 25 ?c -40 ?c 100 105 110 115 120 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) f rc (khz) 128 khz watchdog oscillator frequency vs. temperature 5.5 v 4.0 v 2.7 v 1.8 v 100 102 104 106 108 110 112 114 116 118 -60 -40 -20 0 20 40 60 80 100 temperature (c) f rc (khz)
149 attiny13 2535b?avr?01/04 current consumption of peripheral units figure 122. brownout detector current vs. v cc figure 123. adc current vs. v cc brownout detector current vs. v cc 85 ?c 25 ?c -40 ?c 0 5 10 15 20 25 30 35 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ua) adc current vs. v cc 85 ?c 25 ?c -40 ?c 0 50 100 150 200 250 300 350 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ua)
150 attiny13 2535b?avr?01/04 figure 124. analog comparator current vs. v cc figure 125. programming current vs. v cc analog comparator current vs. v cc 85 ?c 25 ?c -40 ?c 0 20 40 60 80 100 120 140 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ua) programming current vs. vcc 85 c 25 c -40 c 0 0.5 1 1.5 2 2.5 3 3.5 4 1.5 2 2.5 3 3.5 4 4.5 5 5.5 v cc (v) i cc (ma)
151 attiny13 2535b?avr?01/04 current consumption in reset and reset pulse width figure 126. reset supply current vs. v cc (0.1 - 1.0 mhz, excluding current through the reset pull-up) figure 127. reset supply current vs. v cc (1 - 24 mhz, excluding current through the reset pull-up) reset supply current vs. v cc 0.1 - 1.0 mhz, excluding current through the reset pull-up 5.5 v 5.0 v 4.5 v 4.0 v 3.3 v 2.7 v 1.8 v 0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 frequency (mhz) i cc (ma) reset supply current vs. v cc 1 - 24 mhz, excluding current through the reset pull-up 0 0.5 1 1.5 2 2.5 3 3.5 0 2 4 6 8 1012141618202224 frequency (mhz) i cc (ma) 2.7v 1.8v 4.0v 5.0v 4.5v 5.5v 3.3v
152 attiny13 2535b?avr?01/04 figure 128. reset pulse width vs. v cc reset pulse width vs. v cc 0 500 1000 1500 2000 2500 1.8 2.1 2.5 2.7 3 3.3 3.5 4 4.5 5 5.5 6 v cc (v) pulsewidth (ns) 85 oc 25 oc -40 oc
153 attiny13 2535b?avr?01/04 register summary address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 page 0x3f sreg i t h s v n z c page 6 0x3e reserved ? ? ? ? ? ? ? ? 0x3d spl sp[7:0] page 8 0x3c reserved ? 0x3b gimsk ? int0 pcie ? ? ? ? ? page 53 0x3a gifr ?intf0pcif ? ? ? ? ? page 53 0x39 timsk0 ? ? ? ? ocie0b ocie0a toie0 ? page 70 0x38 tifr0 ? ? ? ? ocf0b ocf0a tov0 ? page 71 0x37 spmcsr ? ? ? ctpb rflb pgwrt pgers selfprgen page 97 0x36 ocr0a timer/counter ? output compare register a page 70 0x35 mcucr ? pud se sm1 sm0 ? isc01 isc00 page 49 0x34 mcusr ? ? ? ? wdrf borf extrf porf page 33 0x33 tccr0b foc0a foc0b ? ? wgm02 cs02 cs01 cs00 page 66 0x32 tcnt0 timer/counter (8-bit) page 70 0x31 osccal oscillator calibration register page 22 0x30 reserved ? 0x2f tccr0a com0a1 com0a0 com0b1 com0b0 ? ? wgm01 wgm00 page 69 0x2e dwdr dwdr[7:0] page 94 0x2d reserved ? 0x2c reserved ? 0x2b reserved ? 0x2a reserved ? 0x29 ocr0b timer/counter ? output compare register b page 70 0x28 gtccr tsm ? ? ? ? ? ? psr10 page 73 0x27 reserved ? 0x26 clkpr clkpce ? ? ? clkps3 clkps2 clkps1 clkps0 page 24 0x25 reserved ? 0x24 reserved ? 0x23 reserved ? 0x22 reserved ? 0x21 wdtcr wdtif wdtie wdp3 wdce wde wdp2 wdp1 wdp0 page 37 0x20 reserved ? 0x1f reserved ? 0x1e eearl ? ? eeprom address register page 14 0x1d eedr eeprom data register page 14 0x1c eecr ? ? eepm1 eepm0 eerie eemwe eewe eere page 15 0x1b reserved ? 0x1a reserved ? 0x19 reserved ? 0x18 portb ? ? portb5 portb4 portb3 portb2 portb1 portb0 page 51 0x17 ddrb ? ? ddb5 ddb4 ddb3 ddb2 ddb1 ddb0 page 51 0x16 pinb ? ? pinb5 pinb4 pinb3 pinb2 pinb1 pinb0 page 51 0x15 pcmsk ? ? pcint5 pcint4 pcint3 pcint2 pcint1 pcint0 page 54 0x14 didr0 ? ? adc0d adc2d adc3d adc1d ein1d ain0d page 76, page 91 0x13 reserved ? 0x12 reserved ? 0x11 reserved ? 0x10 reserved ? 0x0f reserved ? 0x0e reserved ? 0x0d reserved ? 0x0c reserved ? 0x0b reserved ? 0x0a reserved ? 0x09 reserved ? 0x08 acsr acd acbg aco aci acie ? acis1 acis0 page 74 0x07 admux ? refs0 adlar ? ? ? mux1 mux0 page 88 0x06 adcsra aden adsc adate adif adie adps2 adps1 adps0 page 89 0x05 adch adc data register high byte page 90 0x04 adcl adc data register low byte page 90 0x03 adcsrb ?acme ? ? ? adts2 adts1 adts0 page 91 0x02 reserved ? 0x01 reserved ? 0x00 reserved ?
154 attiny13 2535b?avr?01/04 note: 1. for compatibility with future devices, reserved bits should be written to zero if accessed. reserved i/o memory addresse s should never be written. 2. i/o registers within the address range 0x00 - 0x1f are directly bit-accessible using the sbi and cbi instructions. in these registers, the value of single bits can be checked by using the sbis and sbic instructions. 3. some of the status flags are cleared by writing a logical one to them. note that, unlike most other avrs, the cbi and sbi instructions will only operation the specified bit, and can therefore be used on registers containing such status flags. the cbi and sbi instructions work with registers 0x00 to 0x1f only.
155 attiny13 2535b?avr?01/04 instruction set summary mnemonics operands description operation flags #clocks arithmetic and logic instructions add rd, rr add two registers rd rd + rr z,c,n,v,h 1 adc rd, rr add with carry two registers rd rd + rr + c z,c,n,v,h 1 adiw rdl,k add immediate to word rdh:rdl rdh:rdl + k z,c,n,v,s 2 sub rd, rr subtract two registers rd rd - rr z,c,n,v,h 1 subi rd, k subtract constant from register rd rd - k z,c,n,v,h 1 sbc rd, rr subtract with carry two registers rd rd - rr - c z,c,n,v,h 1 sbci rd, k subtract with carry constant from reg. rd rd - k - c z,c,n,v,h 1 sbiw rdl,k subtract immediate from word rdh:rdl rdh:rdl - k z,c,n,v,s 2 and rd, rr logical and registers rd rd ? rr z,n,v 1 andi rd, k logical and register and constant rd rd ? kz,n,v1 or rd, rr logical or registers rd rd v rr z,n,v 1 ori rd, k logical or register and constant rd rd v k z,n,v 1 eor rd, rr exclusive or registers rd rd rr z,n,v 1 com rd one?s complement rd 0xff ? rd z,c,n,v 1 neg rd two?s complement rd 0x00 ? rd z,c,n,v,h 1 sbr rd,k set bit(s) in register rd rd v k z,n,v 1 cbr rd,k clear bit(s) in register rd rd ? (0xff - k) z,n,v 1 inc rd increment rd rd + 1 z,n,v 1 dec rd decrement rd rd ? 1 z,n,v 1 tst rd test for zero or minus rd rd ? rd z,n,v 1 clr rd clear register rd rd rd z,n,v 1 ser rd set register rd 0xff none 1 branch instructions rjmp k relative jump pc pc + k + 1 none 2 ijmp indirect jump to (z) pc z none 2 rcall k relative subroutine call pc pc + k + 1 none 3 icall indirect call to (z) pc znone3 ret subroutine return pc stack none 4 reti interrupt return pc stack i 4 cpse rd,rr compare, skip if equal if (rd = rr) pc pc + 2 or 3 none 1/2/3 cp rd,rr compare rd ? rr z, n,v,c,h 1 cpc rd,rr compare with carry rd ? rr ? c z, n,v,c,h 1 cpi rd,k compare register with immediate rd ? k z, n,v,c,h 1 sbrc rr, b skip if bit in register cleared if (rr(b)=0) pc pc + 2 or 3 none 1/2/3 sbrs rr, b skip if bit in register is set if (rr(b)=1) pc pc + 2 or 3 none 1/2/3 sbic p, b skip if bit in i/o register cleared if (p(b)=0) pc pc + 2 or 3 none 1/2/3 sbis p, b skip if bit in i/o register is set if (p(b)=1) pc pc + 2 or 3 none 1/2/3 brbs s, k branch if status flag set if (sreg(s) = 1) then pc pc+k + 1 none 1/2 brbc s, k branch if status flag cleared if (sreg(s) = 0) then pc pc+k + 1 none 1/2 breq k branch if equal if (z = 1) then pc pc + k + 1 none 1/2 brne k branch if not equal if (z = 0) then pc pc + k + 1 none 1/2 brcs k branch if carry set if (c = 1) then pc pc + k + 1 none 1/2 brcc k branch if carry cleared if (c = 0) then pc pc + k + 1 none 1/2 brsh k branch if same or higher if (c = 0) then pc pc + k + 1 none 1/2 brlo k branch if lower if (c = 1) then pc pc + k + 1 none 1/2 brmi k branch if minus if (n = 1) then pc pc + k + 1 none 1/2 brpl k branch if plus if (n = 0) then pc pc + k + 1 none 1/2 brge k branch if greater or equal, signed if (n v= 0) then pc pc + k + 1 none 1/2 brlt k branch if less than zero, signed if (n v= 1) then pc pc + k + 1 none 1/2 brhs k branch if half carry flag set if (h = 1) then pc pc + k + 1 none 1/2 brhc k branch if half carry flag cleared if (h = 0) then pc pc + k + 1 none 1/2 brts k branch if t flag set if (t = 1) then pc pc + k + 1 none 1/2 brtc k branch if t flag cleared if (t = 0) then pc pc + k + 1 none 1/2 brvs k branch if overflow flag is set if (v = 1) then pc pc + k + 1 none 1/2 brvc k branch if overflow flag is cleared if (v = 0) then pc pc + k + 1 none 1/2 brie k branch if interrupt enabled if ( i = 1) then pc pc + k + 1 none 1/2 brid k branch if interrupt disabled if ( i = 0) then pc pc + k + 1 none 1/2 bit and bit-test instructions sbi p,b set bit in i/o register i/o(p,b) 1none2 cbi p,b clear bit in i/o register i/o(p,b) 0none2 lsl rd logical shift left rd(n+1) rd(n), rd(0) 0 z,c,n,v 1 lsr rd logical shift right rd(n) rd(n+1), rd(7) 0 z,c,n,v 1 rol rd rotate left through carry rd(0) c,rd(n+1) rd(n),c rd(7) z,c,n,v 1
156 attiny13 2535b?avr?01/04 ror rd rotate right through carry rd(7) c,rd(n) rd(n+1),c rd(0) z,c,n,v 1 asr rd arithmetic shift right rd(n) rd(n+1), n=0..6 z,c,n,v 1 swap rd swap nibbles rd(3..0) rd(7..4),rd(7..4) rd(3..0) none 1 bset s flag set sreg(s) 1sreg(s)1 bclr s flag clear sreg(s) 0 sreg(s) 1 bst rr, b bit store from register to t t rr(b) t 1 bld rd, b bit load from t to register rd(b) tnone1 sec set carry c 1c1 clc clear carry c 0 c 1 sen set negative flag n 1n1 cln clear negative flag n 0 n 1 sez set zero flag z 1z1 clz clear zero flag z 0 z 1 sei global interrupt enable i 1i1 cli global interrupt disable i 0 i 1 ses set signed test flag s 1s1 cls clear signed test flag s 0 s 1 sev set twos complement overflow. v 1v1 clv clear twos complement overflow v 0 v 1 set set t in sreg t 1t1 clt clear t in sreg t 0 t 1 seh set half carry flag in sreg h 1h1 clh clear half carry flag in sreg h 0 h 1 data transfer instructions mov rd, rr move between registers rd rr none 1 movw rd, rr copy register word rd+1:rd rr+1:rr none 1 ldi rd, k load immediate rd knone1 ld rd, x load indirect rd (x) none 2 ld rd, x+ load indirect and post-inc. rd (x), x x + 1 none 2 ld rd, - x load indirect and pre-dec. x x - 1, rd (x) none 2 ld rd, y load indirect rd (y) none 2 ld rd, y+ load indirect and post-inc. rd (y), y y + 1 none 2 ld rd, - y load indirect and pre-dec. y y - 1, rd (y) none 2 ldd rd,y+q load indirect with displacement rd (y + q) none 2 ld rd, z load indirect rd (z) none 2 ld rd, z+ load indirect and post-inc. rd (z), z z+1 none 2 ld rd, -z load indirect and pre-dec. z z - 1, rd (z) none 2 ldd rd, z+q load indirect with displacement rd (z + q) none 2 lds rd, k load direct from sram rd (k) none 2 st x, rr store indirect (x) rr none 2 st x+, rr store indirect and post-inc. (x) rr, x x + 1 none 2 st - x, rr store indirect and pre-dec. x x - 1, (x) rr none 2 st y, rr store indirect (y) rr none 2 st y+, rr store indirect and post-inc. (y) rr, y y + 1 none 2 st - y, rr store indirect and pre-dec. y y - 1, (y) rr none 2 std y+q,rr store indirect with displacement (y + q) rr none 2 st z, rr store indirect (z) rr none 2 st z+, rr store indirect and post-inc. (z) rr, z z + 1 none 2 st -z, rr store indirect and pre-dec. z z - 1, (z) rr none 2 std z+q,rr store indirect with displacement (z + q) rr none 2 sts k, rr store direct to sram (k) rr none 2 lpm load program memory r0 (z) none 3 lpm rd, z load program memory rd (z) none 3 lpm rd, z+ load program memory and post-inc rd (z), z z+1 none 3 spm store program memory (z) r1:r0 none in rd, p in port rd pnone1 out p, rr out port p rr none 1 push rr push register on stack stack rr none 2 pop rd pop register from stack rd stack none 2 mcu control instructions nop no operation none 1 sleep sleep (see specific descr. for sleep function) none 1 wdr watchdog reset (see specific descr. for wdr/timer) none 1 break break for on-chip debug only none n/a mnemonics operands description operation flags #clocks
157 attiny13 2535b?avr?01/04 ordering information notes: 1. this device can also be supplied in wafer form. please contact your local atmel sales office for detailed ordering info rmation and minimum quantities. 2. pb-free packaging alternative. 3. for speed vs. v cc , see ?maximum speed vs. vcc? on page 116. power supply speed (mhz) ordering code package (1) operation range 12 (3) 1.8 - 5.5 attiny13-12pi attiny13-12pj (2) ATTINY13-12SI attiny13-12sj (2) attiny13-12ssi attiny13-12ssj (2) 8p3 8p3 8s2 8s2 s8s1 s8s1 industrial (-40 c to 85 c) 24 (3) 2.7 - 5.5 attiny13-24pi attiny13-24pj (2) attiny13-24si attiny13-24sj (2) attiny13-24ssi attiny13-24ssj (2) 8p3 8p3 8s2 8s2 s8s1 s8s1 industrial (-40 c to 85 c) package type 8p3 8-lead, 0.300" wide, plastic dual inline package (pdip) 8s2 8-lead, 0.209" wide, plastic gull-wing small outline (eiaj soic) s8s1 8-lead, 0.150" wide, plastic gull-wing small outline (jedec soic)
158 attiny13 2535b?avr?01/04 packaging information 8p3 2325 orchard parkway san jose, ca 95131 title drawing no. r rev. 8p3 , 8-lead, 0.300" wide body, plastic dual in-line package (pdip) 01/09/02 8p3 b d d1 e e1 e l b2 b a2 a 1 n ea c b3 4 plcs top view side view end view common dimensions (unit of measure = inches) symbol min nom max note notes: 1. this drawing is for general information only; refer to jedec drawing ms-001, variation ba for additional information. 2. dimensions a and l are measured with the package seated in jedec seating plane gauge gs-3. 3. d, d1 and e1 dimensions do not include mold flash or protrusions. mold flash or protrusions shall not exceed 0.010 inch. 4. e and ea measured with the leads constrained to be perpendicular to datum. 5. pointed or rounded lead tips are preferred to ease insertion. 6. b2 and b3 maximum dimensions do not include dambar protrusions. dambar protrusions shall not exceed 0.010 (0.25 mm). a 0.210 2 a2 0.115 0.130 0.195 b 0.014 0.018 0.022 5 b2 0.045 0.060 0.070 6 b3 0.030 0.039 0.045 6 c 0.008 0.010 0.014 d 0.355 0.365 0.400 3 d1 0.005 3 e 0.300 0.310 0.325 4 e1 0.240 0.250 0.280 3 e 0.100 bsc ea 0.300 bsc 4 l 0.115 0.130 0.150 2
159 attiny13 2535b?avr?01/04 8s2 2325 orchard parkway san jose, ca 95131 title drawing no. r rev. 8s2 , 8-lead, 0.209" body, plastic small outline package (eiaj) 10/7/03 8s2 c common dimensions (unit of measure = mm) symbol min nom max note notes: 1. this drawing is for general information only; refer to eiaj drawing edr-7320 for additional information. 2. mismatch of the upper and lower dies and resin burrs are not included. 3. it is recommended that upper and lower cavities be equal. if they are different, the larger dimension shall be regarded. 4. determines the true geometric position. 5. values b and c apply to pb/sn solder plated terminal. the standard thickness of the solder layer shall be 0.010 +0.010/ ? 0.005 mm. a 1.70 2.16 a1 0.05 0.25 b 0.35 0.48 5 c 0.15 0.35 5 d 5.13 5.35 e1 5.18 5.40 2, 3 e 7.70 8.26 l 0.51 0.85 ? 0? 8? e 1.27 bsc 4 end view side view e b a a1 d e n 1 c e1 ? l top view
160 attiny13 2535b?avr?01/04 s8s1 2325 orchard parkway san jose, ca 95131 title drawing no. r rev. s8s1 , 8-lead, 0.150" wide body, plastic gull wing small outline (jedec soic) 7/28/03 s8s1 a common dimensions (unit of measure = mm) symbol min nom max note notes: 1. this drawing is for general information only; refer to jedec drawing ms-012 for proper dimensions, tolerances, datums,e tc. e 5.79 6.20 e1 3.81 3.99 a 1.35 1.75 a1 0.1 0.25 d 4.80 4.98 c 0.17 0.25 b 0.31 0.51 l 0.4 1.27 e 1.27 bsc 0 o 8 o top view side view end view 1 n c a a1 b l e d e1 e
161 attiny13 2535b?avr?01/04 errata the revision letter in this section refers to the revision of the attiny13 device. attiny13 rev. c there are no known errata for this revision. attiny13 rev. b  wrong values read after erase only operation  high voltage serial programming flash, eeprom, fuse and lock bits may fail  device may lock for further programming  debugwire communication not blocked by lock-bits  watchdog timer interrupt disabled 1. wrong values read after erase only operation at supply voltages below 2.7 v, an eeprom location that is erased by the erase only operation may read as programmed (0x00). problem fix/workaround if it is necessary to read an eeprom location after erase only, use an atomic write operation with 0xff as data in order to erase a location. in any case, the write only operation can be used as intended. thus no special considerations are needed as long as the erased location is not read before it is programmed. 2. high voltage serial programming flash, eeprom, fuse and lock bits may fail writing to any of these locations and bits may in some occasions fail. problem fix/workaround after a writing has been initiated, always observe the rdy/bsy signal. if the writing should fail, rewrite until the rdy/bsy verifies a correct writing. this will be fixed in revision c. 3. device may lock for further programming special combinations of fuse bits will lock the device for further programming effec- tively turning it into an otp device. the following combinations of settings/fuse bits will cause this effect: ? 128 khz internal oscillator (cksel[1..0] = 11), shortest start-up time (sut[1..0] = 00), debugwire enabled (dwen = 0) or reset disabled rstdisbl = 0. ? 9.6 mhz internal oscillator (cksel[1..0] = 10), shortest start-up time (sut[1..0] = 00), debugwire enabled (dwen = 0) or reset disabled rstdisbl = 0. ? 4.8 mhz internal oscillator (cksel[1..0] = 01), shortest start-up time (sut[1..0] = 00), debugwire enabled (dwen = 0) or reset disabled rstdisbl = 0. problem fix/ workaround avoid the above fuse combinations. selecting longer start-up time will eliminate the problem. 4. debugwire communication not blocked by lock-bits when debugwire on-chip debug is enabled (dwen = 0), the contents of program memory and eeprom data memory can be read even if the lock-bits are set to block further reading of the device.
162 attiny13 2535b?avr?01/04 problem fix/ workaround do not ship products with on-chip debug of the tiny13 enabled. 5. watchdog timer interrupt disabled if the watchdog timer interrupt flag is not cleared before a new timeout occurs, the watchdog will be disabled, and the interrupt flag will automatically be cleared. this is only applicable in interrupt only mode. if the watchdog is configured to reset the device in the watchdog time-out following an interrupt, the device works correctly. problem fix / workaround make sure there is enough time to always service the first timeout event before a new watchdog timeout occurs. this is done by selecting a long enough time-out period. attiny13 rev. a revision a has not been sampled.
163 attiny13 2535b?avr?01/04 datasheet change log for attiny13 please note that the referring page numbers in this section are referring to this docu- ment. the referring revision in this section are referring to the document revision. changes from rev. 2535a-06/03 to rev. 2535b-01/04 1. updated figure 2 on page 2. 2. updated table 12 on page 30, table 17 on page 39, table 37 on page 89 and table 57 on page 116. 3. updated ?calibrated internal rc oscillator? on page 22. 4. updated the whole ?watchdog timer? on page 35. 5. updated figure 53 on page 103 and figure 56 on page 108. 6. updated registers ?mcu control register ? mcucr? on page 49, ?timer/counter control register b ? tccr0b? on page 69 and ?digital input disable register 0 ? didr0? on page 76. 7. updated absolute maximum ratings and dc characteristics in ?electrical characteristics? on page 115. 8. added ?maximum speed vs. vcc? on page 116 9. updated ?adc characteristics ? preliminary data? on page 118. 10. updated ?attiny13 typical characteristics ? preliminary data? on page 119. 11. updated ?ordering information? on page 157. 12. updated ?packaging information? on page 158. 13. updated ?errata? on page 161. 14. changed instances of eear to eearl.
164 attiny13 2535b?avr?01/04
i attiny13 2535b?avr?01/04 table of contents features................................................................................................ 1 pin configurations............................................................................... 1 overview............................................................................................... 2 block diagram ...................................................................................................... 2 pin descriptions.................................................................................................... 3 about code examples......................................................................... 3 avr cpu core ..................................................................................... 4 introduction ........................................................................................................... 4 architectural overview.......................................................................................... 4 alu ? arithmetic logic unit.................................................................................. 5 status register ..................................................................................................... 6 general purpose register file ............................................................................. 7 stack pointer ........................................................................................................ 8 instruction execution timing................................................................................. 9 reset and interrupt handling................................................................................ 9 avr attiny13 memories ................................................................... 12 in-system re-programmable flash program memory ....................................... 12 sram data memory........................................................................................... 13 eeprom data memory...................................................................................... 14 i/o memory ......................................................................................................... 19 system clock and clock options .................................................... 20 clock systems and their distribution .................................................................. 20 clock sources..................................................................................................... 21 default clock source .......................................................................................... 21 calibrated internal rc oscillator ........................................................................ 22 external clock..................................................................................................... 23 128 khz internal oscillator.................................................................................. 24 system clock prescaler...................................................................................... 24 power management and sleep modes............................................. 26 idle mode ............................................................................................................ 27 adc noise reduction mode............................................................................... 27 power-down mode.............................................................................................. 27 minimizing power consumption ......................................................................... 28 system control and reset ................................................................ 29 internal voltage reference ................................................................................. 34 watchdog timer ................................................................................................. 35
ii attiny13 2535b?avr?01/04 interrupts ............................................................................................ 40 interrupt vectors in attiny13 .............................................................................. 40 i/o ports.............................................................................................. 41 introduction ......................................................................................................... 41 ports as general digital i/o ................................................................................ 42 alternate port functions ..................................................................................... 46 register description for i/o-ports....................................................................... 51 external interrupts ............................................................................. 52 8-bit timer/counter0 with pwm........................................................ 55 overview............................................................................................................. 55 timer/counter clock sources............................................................................. 56 counter unit........................................................................................................ 56 output compare unit.......................................................................................... 57 compare match output unit ............................................................................... 59 modes of operation ............................................................................................ 60 timer/counter timing diagrams......................................................................... 64 8-bit timer/counter register description ........................................................... 66 timer/counter prescaler ................................................................... 72 analog comparator ........................................................................... 74 analog comparator multiplexed input ................................................................ 76 analog to digital converter .............................................................. 77 features.............................................................................................................. 77 operation ............................................................................................................ 78 starting a conversion ......................................................................................... 79 prescaling and conversion timing ..................................................................... 80 changing channel or reference selection ........................................................ 83 adc noise canceler........................................................................................... 84 adc conversion result...................................................................................... 88 debugwire on-chip debug system ................................................ 93 features.............................................................................................................. 93 overview............................................................................................................. 93 physical interface ............................................................................................... 93 software break points ........................................................................................ 94 limitations of debugwire .................................................................................. 94 debugwire related register in i/o memory ..................................................... 94 self-programming the flash............................................................. 95 addressing the flash during self-programming ................................................ 96
iii attiny13 2535b?avr?01/04 memory programming..................................................................... 100 program and data memory lock bits .............................................................. 100 fuse bytes........................................................................................................ 101 signature bytes ................................................................................................ 102 calibration byte ................................................................................................ 102 page size ......................................................................................................... 102 serial downloading........................................................................................... 103 high-voltage serial programming..................................................................... 107 high-voltage serial programming algorithm..................................................... 109 high-voltage serial programming characteristics ............................................ 114 electrical characteristics................................................................ 115 absolute maximum ratings*............................................................................. 115 dc characteristics............................................................................................ 115 external clock drive waveforms ...................................................................... 116 external clock drive ......................................................................................... 116 maximum speed vs. v cc .................................................................................. 116 adc characteristics ? preliminary data........................................................... 118 attiny13 typical characteristics ? preliminary data ................... 119 active supply current ....................................................................................... 119 idle supply current ........................................................................................... 122 power-down supply current ............................................................................ 125 pin pull-up ........................................................................................................ 126 pin driver strength ........................................................................................... 128 pin thresholds and hysteresis ......................................................................... 137 bod thresholds and analog comparator offset ............................................. 142 internal oscillator speed .................................................................................. 145 current consumption of peripheral units ......................................................... 149 current consumption in reset and reset pulse width..................................... 151 register summary ........................................................................... 153 instruction set summary ................................................................ 155 ordering information....................................................................... 157 packaging information .................................................................... 158 8p3 ................................................................................................................... 158 8s2 ................................................................................................................... 159 s8s1 ................................................................................................................. 160 errata ................................................................................................ 161 attiny13 rev. c................................................................................................ 161 attiny13 rev. b................................................................................................ 161 attiny13 rev. a................................................................................................ 162
iv attiny13 2535b?avr?01/04 datasheet change log for attiny13 .............................................. 163 changes from rev. 2535a-06/03 to rev. 2535b-01/04 ................................... 163 table of contents ................................................................................. i
printed on recycled paper. disclaimer: atmel corporation makes no warranty for the use of its products , other than those expressly contained in the company?s standar d warranty which is detailed in atmel?s terms and conditions located on the company?s web site. the company assumes no responsibi lity for any errors which may appear in this document, reserves the right to change devices or specifications detailed herein at any time wi thout notice, and does not make any commitment to update the information contained herein. no licenses to patents or other intellectual property of atmel are granted by the company in connection with the sale of atmel produc ts, expressly or by implication. atmel?s products are not aut horized for use as critical components in life support devices or systems. atmel corporation atmel operations 2325 orchard parkway san jose, ca 95131, usa tel: 1(408) 441-0311 fax: 1(408) 487-2600 regional headquarters europe atmel sarl route des arsenaux 41 case postale 80 ch-1705 fribourg switzerland tel: (41) 26-426-5555 fax: (41) 26-426-5500 asia room 1219 chinachem golden plaza 77 mody road tsimshatsui east kowloon hong kong tel: (852) 2721-9778 fax: (852) 2722-1369 japan 9f, tonetsu shinkawa bldg. 1-24-8 shinkawa chuo-ku, tokyo 104-0033 japan tel: (81) 3-3523-3551 fax: (81) 3-3523-7581 memory 2325 orchard parkway san jose, ca 95131, usa tel: 1(408) 441-0311 fax: 1(408) 436-4314 microcontrollers 2325 orchard parkway san jose, ca 95131, usa tel: 1(408) 441-0311 fax: 1(408) 436-4314 la chantrerie bp 70602 44306 nantes cedex 3, france tel: (33) 2-40-18-18-18 fax: (33) 2-40-18-19-60 asic/assp/smart cards zone industrielle 13106 rousset cedex, france tel: (33) 4-42-53-60-00 fax: (33) 4-42-53-60-01 1150 east cheyenne mtn. blvd. colorado springs, co 80906, usa tel: 1(719) 576-3300 fax: 1(719) 540-1759 scottish enterprise technology park maxwell building east kilbride g75 0qr, scotland tel: (44) 1355-803-000 fax: (44) 1355-242-743 rf/automotive theresienstrasse 2 postfach 3535 74025 heilbronn, germany tel: (49) 71-31-67-0 fax: (49) 71-31-67-2340 1150 east cheyenne mtn. blvd. colorado springs, co 80906, usa tel: 1(719) 576-3300 fax: 1(719) 540-1759 biometrics/imaging/hi-rel mpu/ high speed converters/rf datacom avenue de rochepleine bp 123 38521 saint-egreve cedex, france tel: (33) 4-76-58-30-00 fax: (33) 4-76-58-34-80 literature requests www.atmel.com/literature 2535b?avr?01/04 ? atmel corporation 2004 . all rights reserved. atmel ? and combinations thereof, avr ? , and avr studio ? are the registered trademarks of atmel corporation or its subsidiaries. microsoft ? , windows ? , windows nt ? , and windows xp ? are the registered trademarks of microsoft corpo- ration. other terms and product names may be the trademarks of others


▲Up To Search▲   

 
Price & Availability of ATTINY13-12SI

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X